foreach (DataRow udr in udt.Rows)
                                        {
                                            defaultval = udr["result"].ToString();
                                            cmb.SelectedText = defaultval;
                                            cmb.Select();
                                             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.

Dani AI

Generated

Quick diagnosis and a safe workflow that fits the existing posts: ’s loop sets the combo repeatedly and uses SelectedText — that won’t reliably choose an item from a bound list. Bind the drop-down once, then set the selection based on the value stored in udt (the value in udt.Rows[0]["result"]). Whether that value is the DetailID (preferred) or the display text (ListVal) changes the approach.

If udt.result is the DetailID (numeric), set the selected value after binding:

var desiredId = Convert.ToInt32(udt.Rows[0]["result"]);  // get the id from udt
// (assume ddtable already bound to the combo)
comboBox1.SelectedValue = desiredId;

If udt.result is the display text, find the matching row in ddtable and select its id:

var defaultText = udt.Rows[0]["result"].ToString();
var match = ddtable.AsEnumerable()
    .FirstOrDefault(r => string.Equals(r.Field<string>("ListVal"), defaultText, StringComparison.OrdinalIgnoreCase));
if (match != null)
    comboBox1.SelectedValue = match.Field<object>("DetailID");
else
    comboBox1.Text = defaultText; // show text when no match, or add the value to ddtable before binding

Troubleshooting checklist: set the selection only after DataSource is assigned; make sure ValueMember type matches the id type (int vs string); verify column names exactly; avoid setting DataSource inside a loop; consider using a BindingSource and BindingSource.Find(...) to locate positions. ’s idea of returning the default value from the query and ’s suggestion to insert a top row are both valid alternatives if it’s easier to include the default item in the bound list before binding.

Recommended Answers

All 6 Replies

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.

Hi

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.

HTH

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);
d.Fill(t);
combobox1.Items.Clear();
for(int a=0;a<t.Rows.Count;a++)
{
    combobox1.Items.Add(t.Rows[a][0].ToString());
}

God Bless and Enjoy Programming. O:)

none of these working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.