|
I've never done it like this.
I'm not saying it's wrong just that I haven't done it like this.
Usually I create a array[] or list of something(string, int, custom class), load the datas from the db in the list or array through a datareader,
then I use the Items.AddRange() method of the combobox and specify theArray or the theList.ToArray()
All the best,
Dan
|
|
|
|
|
Yes, that definitely is a solution although I've always just considered it more elegant to bind the combobox's datasource directly to the queried datasource.
|
|
|
|
|
Dewald wrote: please let me know so I can start doing it differently
I used any one way based on condition to bind database with Combobox. The two different way are given below
SqlCommand cmd = new SqlCommand("Query",connetion);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr[index].ToString());
}
and
SqlCommand cmd = new SqlCommand("Query",connetion);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,tablename);
DataTable dt = ds.Tables[tablename];
DataRow dr;
int x = 0;
while (x != dt.Rows.Count)
{
dr = dt.Rows[x];
comboBox1.Items.Add(dr[index].ToString());
x++;
}
let us know in case of any query
modified on Wednesday, January 19, 2011 8:14 AM
|
|
|
|
|
Dewald wrote: Can anyone tell me of a way to make sure that the ComboBox is bound to the DB before I try to change the SelectedValue explicitly?
I'm not sure if this a winform, but isn't there a databound event. Just create a method for you cb.databound += YourMethod() and put your code there. Also, you can check the items count before setting a value.
Good luck.
I didn't get any requirements for the signature
|
|
|
|
|
No, there is no databound event, at least not that I am aware of. The DataGridView control, for instance, has a DataBindingComplete event which is what I usually use for this when working with a grid view but with this ComboBox I am stumped.
I can check the items count before setting a value but at that point I wouldn't necessarily know what the count should be. I mean, it's one thing if the count is 0, then I know the binding hasn't finished but if it's not 0, all I know is that the binding is at least partially finished but I can't be sure that it's totally finished - if that makes sense.
At any rate, I can run through the SqlDataReader and add the entries one by one, that way I know when I've added all of them. I just don't like that approach all that much as it seems a bit brute.
|
|
|
|
|
Dewald wrote: At any rate, I can run through the SqlDataReader and add the entries one by one, that way I know when I've added all of them. I just don't like that approach all that much as it seems a bit brute.
There are so many ways you can do databiding. This method is probably the most efficient way, if you aren't caching the data. And there is no magic or generated code. It is easy to read and debug. There's nothing wrong with iterating a datareader.
I didn't get any requirements for the signature
|
|
|
|
|
Have you seen if the DataTable has rows or is empty? Maybe the SELECT instruction is wrong.
|
|
|
|
|
Yes I have, that's the first thing I checked. The ComboBox does have all the values that I expect it to have, it's just that I can't set the value to anythign specific immediately after binding it to the DB.
When I put a button on the form and add code to it's Click event to sete the ComboBox value to a specific value it works. It's just when I try to do it immediately after doing the binding that it doesn't.
|
|
|
|
|
Is there a good reason to use a SqlDataReader for filling a DataTable into a DataSet instead of using a SqlDataAdapter?
|
|
|
|
|
Hi Dewald,
Not sure if you have found a solution to this as yet but this is what I do when I want to populate a comboBox from a db.
try
{
conn.Open();
string sql = @"select Titles, TitleID from tblXXXXXXXX";
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Titles";
comboBox1.ValueMember = "Titles";
this.BindingContext[dt, "Titles"].Position = 0;
}
Hope this helps.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Dankie Kwagga,
I've never actually used a DataAdapter, I don't know why. So I'll definitely give this a try, thanks.
|
|
|
|
|
ok,
so here is what i am trying to accomplish, and i've had some "OK" ideas, but none seem to fit the bill exactly...
Program Logic:
i have a file, that will be read and parsed into a datatable
(this is at runtime, columns are unknown & generated as the file as parsed... the number of columns can change..)
then in my application, a certain set of criteria will be applied to that file, if col1 = "asdf", if col2 = "blue"
if all the conditions are met, the file is moved to dir A, if the conditions are not met, the file is moved to dir B.
Problem:
the problem comes when the criteria are applied, basically i want the end user to be able to create their own criteria for the file, the end user will not know SQL so i was thinking some kind of graphical representation of the WHERE clause....
there are a couple of problems i've ran into:
how would the user specify AND/OR to multiple conditions: (they can have infinite #)
(graphically represented:
IF col1 = "BLUE" AND
IF col2 = "Green" OR
IF col3 = "yellow"
could render 2 sets SQL Where clauses:
1. WHERE ((col1='blue' AND col2='green') OR col3='yellow')
2. WHERE (col1='blue' AND (col2='green' OR col3='yellow')
my solution for this problem was to setup AND/OR condition groups:
Graphically represented:
IF all of the following are true
(
col1='blue'
col2='green'
)
OR
IF all of the following are true
(
col3='yellow'
)
this is easy to represent graphically, but i can't find a good solution for storing it long term in the database...
i'd rather not have to write an entire application to generate/parse SQL into this format
does anyone have any ideas? / are the some code snippets available that do something similar?
|
|
|
|
|
I'm not sure of all you're proposing, but I never allow data from a file to be loaded into a database simply based on the sender's say-so. I've been asked to implement such things more than once. Don't give out information willy-nilly -- the sender shouldn't know the names of the columns in the table, nor even what database system you use.
I very much prefer to have a method of validation and mapping under my own care that directs the file import process.
|
|
|
|
|
in a standard database-type system this is true, however the entire point of the application is moving files from one folder to either of two ending folders, so the datatable i mentioned is just a temporary one.
|
|
|
|
|
You need to develop what's called an ad-hoc query interface. I'd probably create a form that contained the following controls in a row:
Label (with column name)
ComboBox (allows the user to select an operator, like <, = >, etc - "=" is the default selection)
TextBox (allows the user to specify the value used in the expression)
ComboBox (allows the user to specify END, AND or OR - END is the default selection)
If the user selected AND or OR in the last combobox, add another row of controls to the form.
When the user indicates that he's done specifying parameters, create a LINQ query that conforms to the specified parameters.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
ah, i knew it must have a name!!
that makes sense, and i have come to that conclusion, but i want to make it very advanced, and allow the user to have the ability for nested AND/OR clauses to produce SQL like this:
WHERE (col1 = "a" OR ((col2="b" or col3 = "c") AND (col4="d"))
is there an example of an ad-hoc query engine on codeproject, that i could look at?
|
|
|
|
|
Do you have the ability to type "google.com" in your browser's address bar?
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
In my opinion, a better answer would be...
No I don't, but I'm sure a Google search will yield some useful results.
Says the same thing but without the unnecessary rudeness
|
|
|
|
|
Of course, you're assuming I wanted to avoid being rude, or that I wasn't matter-of-factly posing a reasonable question.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
John Simmons / outlaw programmer wrote: Of course, you're assuming I wanted to avoid being rude, or that I wasn't matter-of-factly posing a reasonable question
you don't think i have searched google?, i actualy assumed that the user answering the thread would have some more in depth knowledge other than "this is what its called..."
there is no need to be rude, that only gives yourself a bad name.
|
|
|
|
|
There is a function with byte** parameter taking array of pointers to byte array.
How to pin pointers to managed list of byte[] arrays and pass it that unmanaged function?
Чесноков
|
|
|
|
|
You pin a byte[] by using the fixed keyword within an unsafe code block
Please also refer to CP article:
This[^]
|
|
|
|
|
That is not single byte[] array.
There is a list of byte[] arrays List<byte[]>.
The unmanaged function takes byte** argument the array of pointers to byte* arrays.
Чесноков
|
|
|
|
|
Can you show us the signature of the unmanaged function?
|
|
|
|
|
I forgot how to handle that situation in C#.
It needs to allocate with static alloc array of pointers then pin byte[] from List to that array.
void function(byte** data, unsigned int N, unsigned int M)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
printf("%c ", data[i][j]);
printf("\n");
}
}
The functions takes array of pointers to byte* arrays.
In C# those byte[] arrays are stored in the List<byte[]> collection.
Чесноков
|
|
|
|