DataBindingComplete on ComboBox?
-
Hi all, I'm experiencing something odd (well, to me at least but I'm sure there is a good explanation for it). I have a ComboBox on my form which I populate as follows:
using (SqlCommand myCommand = new SqlCommand("SELECT Value, Description FROM ...", myConnection))
{
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(myReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
myComboBox.DataSource = myDataSource.Tables[0];
myComboBox.DisplayMember = "Description";
myComboBox.ValueMember = "Value";
}
}This is the way I've always used to populate ComboBoxes from a DB. If it's wrong, please let me know so I can start doing it differently, although it has worked for me so far. The problem I'm getting is that I'm trying to set the SelectedValue of the ComboBox immediately after the above block of code with something like
myComboBox.SelectedValue = myDefaultValue; //myDefaultValue is of course a value that I know will be in the list returned by the original query
but it does nothing. When I step through the code I notice that by the time the above line executes the value of myComboBox.Items.Count is still 0 so it appears as if the binding has not finished yet, but the ComboBox does not have a DataBindingComplete() event. 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'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
-
Hi all, I'm experiencing something odd (well, to me at least but I'm sure there is a good explanation for it). I have a ComboBox on my form which I populate as follows:
using (SqlCommand myCommand = new SqlCommand("SELECT Value, Description FROM ...", myConnection))
{
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(myReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
myComboBox.DataSource = myDataSource.Tables[0];
myComboBox.DisplayMember = "Description";
myComboBox.ValueMember = "Value";
}
}This is the way I've always used to populate ComboBoxes from a DB. If it's wrong, please let me know so I can start doing it differently, although it has worked for me so far. The problem I'm getting is that I'm trying to set the SelectedValue of the ComboBox immediately after the above block of code with something like
myComboBox.SelectedValue = myDefaultValue; //myDefaultValue is of course a value that I know will be in the list returned by the original query
but it does nothing. When I step through the code I notice that by the time the above line executes the value of myComboBox.Items.Count is still 0 so it appears as if the binding has not finished yet, but the ComboBox does not have a DataBindingComplete() event. 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?
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
// Connected Environment
SqlCommand cmd = new SqlCommand("Query",connetion);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr[index].ToString());
}and
// Disconnected Environment
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());//index may be 0,1 etc based on your condition
x++;
}let us know in case of any query :)
modified on Wednesday, January 19, 2011 8:14 AM
-
Hi all, I'm experiencing something odd (well, to me at least but I'm sure there is a good explanation for it). I have a ComboBox on my form which I populate as follows:
using (SqlCommand myCommand = new SqlCommand("SELECT Value, Description FROM ...", myConnection))
{
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(myReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
myComboBox.DataSource = myDataSource.Tables[0];
myComboBox.DisplayMember = "Description";
myComboBox.ValueMember = "Value";
}
}This is the way I've always used to populate ComboBoxes from a DB. If it's wrong, please let me know so I can start doing it differently, although it has worked for me so far. The problem I'm getting is that I'm trying to set the SelectedValue of the ComboBox immediately after the above block of code with something like
myComboBox.SelectedValue = myDefaultValue; //myDefaultValue is of course a value that I know will be in the list returned by the original query
but it does nothing. When I step through the code I notice that by the time the above line executes the value of myComboBox.Items.Count is still 0 so it appears as if the binding has not finished yet, but the ComboBox does not have a DataBindingComplete() event. 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?
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
-
Hi all, I'm experiencing something odd (well, to me at least but I'm sure there is a good explanation for it). I have a ComboBox on my form which I populate as follows:
using (SqlCommand myCommand = new SqlCommand("SELECT Value, Description FROM ...", myConnection))
{
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(myReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
myComboBox.DataSource = myDataSource.Tables[0];
myComboBox.DisplayMember = "Description";
myComboBox.ValueMember = "Value";
}
}This is the way I've always used to populate ComboBoxes from a DB. If it's wrong, please let me know so I can start doing it differently, although it has worked for me so far. The problem I'm getting is that I'm trying to set the SelectedValue of the ComboBox immediately after the above block of code with something like
myComboBox.SelectedValue = myDefaultValue; //myDefaultValue is of course a value that I know will be in the list returned by the original query
but it does nothing. When I step through the code I notice that by the time the above line executes the value of myComboBox.Items.Count is still 0 so it appears as if the binding has not finished yet, but the ComboBox does not have a DataBindingComplete() event. 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'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
-
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.
-
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.
-
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
-
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.
-
Hi all, I'm experiencing something odd (well, to me at least but I'm sure there is a good explanation for it). I have a ComboBox on my form which I populate as follows:
using (SqlCommand myCommand = new SqlCommand("SELECT Value, Description FROM ...", myConnection))
{
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataSet myDataSet = new DataSet();
DataTable myDataTable = new DataTable();
myDataSet.Tables.Add(myDataTable);
myDataSet.Load(myReader, LoadOption.PreserveChanges, myDataSource.Tables[0]);
myComboBox.DataSource = myDataSource.Tables[0];
myComboBox.DisplayMember = "Description";
myComboBox.ValueMember = "Value";
}
}This is the way I've always used to populate ComboBoxes from a DB. If it's wrong, please let me know so I can start doing it differently, although it has worked for me so far. The problem I'm getting is that I'm trying to set the SelectedValue of the ComboBox immediately after the above block of code with something like
myComboBox.SelectedValue = myDefaultValue; //myDefaultValue is of course a value that I know will be in the list returned by the original query
but it does nothing. When I step through the code I notice that by the time the above line executes the value of myComboBox.Items.Count is still 0 so it appears as if the binding has not finished yet, but the ComboBox does not have a DataBindingComplete() event. 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?
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.
-
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.