hi,
pls. tell me how to select value from a check box, & from a list box or combo box.
actually i m new to c# , so sorry for this stupid question but i need your help.
Thanx in advance.
Happy Diwali
Regards
~ Dimple

Dani AI

Generated

Builds on the pointers from , and with practical patterns that work smoothly in real WinForms apps: typed binding for list/combos, using the control Tag to carry an ID for checkboxes, and a safe way to send selected GridView rows to the database.

Bind a list of typed objects so the UI shows friendly text while code uses the ID:

class Option { public int Id; public string Label; }
comboBox.DataSource = optionsList;
comboBox.DisplayMember = "Label";
comboBox.ValueMember = "Id";

// safely read the chosen object
var chosen = comboBox.SelectedItem as Option;
if (chosen != null) {
    int id = chosen.Id;
    string text = chosen.Label;
}

For multiple choice rendered as checkboxes, attach the underlying value to the control so event handlers can update the model without parsing text:

var cb = new CheckBox { Text = item.Label, Tag = item.Id };
cb.CheckedChanged += (s,e) => {
    var id = (int)((CheckBox)s).Tag;
    var on = ((CheckBox)s).Checked;
    // update backing data (list/db) for id => on
};
panel.Controls.Add(cb);

To collect checked rows from a DataGridView and update the DB, gather IDs first and send them as a structured parameter (avoids string concatenation and SQL injection):

var ids = new List<int>();
foreach (DataGridViewRow r in dgv.Rows)
    if (Convert.ToBoolean(r.Cells["Select"].Value))
        ids.Add(Convert.ToInt32(r.Cells["Id"].Value));

var dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
foreach (var i in ids) dt.Rows.Add(i);
// pass dt as a SqlParameter with SqlDbType.Structured to a stored proc that accepts a TVP

Troubleshooting: always null-check selections, keep UI logic separate from data access, and prefer parameterized/structured DB calls for bulk updates. These patterns scale better than reading control text or building inline SQL.

Recommended Answers

All 6 Replies

For checkBox, use Checked and CheckState property.
List and ComboBox; use SelectedValue property.

commented: good suggestion +5

The example below will retrieve the value of a checkbox and display it in a messagebox on a button click event.

if (checkBox1.Checked)
            {
                MessageBox.Show(checkBox1.Text.ToString());
            }

It wasn't clear to me whether your question was for obtaining the state, as you have already been directed, or you are wanting to set the state, which I will throw out some additional direction below.

// ListBox method to select an item...
public void SetSelected(int index, bool value);
public ListBox.SelectedObjectCollection SelectedItems { get; } // to see all items selected...
// and, to capture when the selection is changing:
public event EventHandler SelectedIndexChanged;

// RadioButton method to get/set state:
public bool Checked { get; set; }
// and, to capture when the state has been changed:
public event EventHandler CheckedChanged;

// ComboBox methods to select item:
public override int SelectedIndex { get; set; }
public object SelectedItem { get; set; }
// and, to capture when the selection has changed:
public event EventHandler SelectedIndexChanged;

If you search on these, you should be able to find all the example code you need to see/understand how these are implemented in a form.

Cheers!

Hey
Thanks to all.

how to gridview selected checkbox row send (pass) to same sqldatabase

Please mark this thread as solved, if your problem is solved.......
Thank you..

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.