Getting data from selected datagridview row and which event?

后端 未结 5 1356
眼角桃花
眼角桃花 2020-12-08 06:53

I have a DataGridView (Selectionmode: FullRowSelect) on a windows form along with some textboxes, so what i want to do is that whenever a user selects a row(click or double_

5条回答
  •  失恋的感觉
    2020-12-08 07:20

    You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:

    private void dataGridView_SelectionChanged(object sender, EventArgs e) 
    {
        foreach (DataGridViewRow row in dataGridView.SelectedRows) 
        {
            string value1 = row.Cells[0].Value.ToString();
            string value2 = row.Cells[1].Value.ToString();
            //...
        }
    }
    

    You can also walk through the column collection instead of typing indexes...

提交回复
热议问题