foreach (DataRow udr in udt.Rows)
                                            defaultval = udr["result"].ToString();
                                            cmb.SelectedText = defaultval;
                                             cmb.DataSource = ddtable;
                                        cmb.DisplayMember = "ListVal";
                                        cmb.ValueMember = "DetailID";

here problem is that i want to put defaultval at selectedindex 0,here default value is taken from anothe table.
i mean i have send a combo selected value into database for edit purpose i have to retrive this data at combo index position 1.
please help me.
thanks for help in advance.

Rather than bind the combo box values directly to your table, built a list in memory with the contents of the table and any other extra values you want. Then bind to the list.

Alternatively, you can adjust your select statement for ddtable to include selection of the default value from the other table so that it's part of the result and no special logic is needed in your program.


In addition to deceptikon's suggestion, I just wanted to point out that if you are data binding to a DataTable then you do not need to enumerate each row and continuously update the DataSource.

The following is enough to bind a DataTable to a Combo Box:

comboBox1.DisplayMember = "ListVal";
comboBox1.ValueMember = "DetailID";
comboBox1.DataSource = ddtable;

Another option for your default value would be to insert a new row to your ddtable Data Table based on the value of your "result" field. If you do this before you databind the Data Table using the above code then the table will contain the default value at the beginning of the list. For example:

    DataRow row = ddtable.NewRow();
    row["ListVal"] = "Default Value"; //You would get this from the result field in ddtable
    row["DetailID"] = -1;
    ddtable.Rows.InsertAt(row, 0);

How you get the result value depends on how it is currently stored in the ddtable Data Table, but if it is say the first row then a simple row["ListVal"] = ddtable.Rows[0]["result"].ToString(); should suffice.


here udt and ddtable are different tables, udt contains field result while ddtable contains field listval.

here udt and ddtable are different tables, udt contains field result while ddtable contains field listval.

Ok, but you still only need to data bind once, and not in a For Loop.

In terms of the udt table containing the result, how many rows are in this table? If it is just one then you can still use the same concept. But instead of reading from ddtable, read from udt row["ListVal"] = udt.Rows[0]["result"].ToString();. Then ddTable contains the result value as the first record and the remainder of the data as is which is then all bound to the combo box.

Required Variabel

SqlConnection c = new SqlConnection("your connection");
SqlDataApdapter d = new SqlDataApdapter()
String q;
DataTable t;

Query to insert data to combobox

q = "select field from table";
t = new DataTable();
d.SelectCommand = new SqlCommand(q, c);
for(int a=0;a<t.Rows.Count;a++)

God Bless and Enjoy Programming. O:)