0

I want to get value of row where checkbox is checked. I am new to C# windows forms and so far unsuccessful. I want to eventually use these row values, so if user selects multiple row and then I should get value for those checked. Also, I have set the selection mode to 'fullrowselect'

Please suggest changes to my code

private void button1_Click(object sender, EventArgs e)
{
    StringBuilder ln = new StringBuilder();
    dataGridView1.ClearSelection();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (dataGridView1.SelectedRows.Count>0 )
        {                    
            ln.Append(row.Cells[1].Value.ToString());
        }
        else
        {
            MessageBox.Show("No row is selected!");
            break;                    
        }
    }
    MessageBox.Show("Row Content -" + ln);
}
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
user3613720
  • 47
  • 1
  • 9

3 Answers3

0

The SelectedRows is the count of rows that are selected (or highlited rows), and not the rows that are checked. The second problem in your code is that you're doing an foreach row, but inside you're using the dataGridView, and not the current row. This is how your if must be:

private void button1_Click(object sender, EventArgs e)
{
    const int checkBoxPosition = 3; //You must type here the position of checkbox.
                                    // Remember, it's zero based.

    StringBuilder ln = new StringBuilder();
    dataGridView1.ClearSelection();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
      if (row.Cells[checkBoxPosition].Value == true)
        {                    
            ln.Append(row.Cells[1].Value.ToString());
        }
        else
        {
            MessageBox.Show("No row is selected!");
            break;                    
        }

    }
    MessageBox.Show("Row Content -" + ln);
}
Ricardo Silva
  • 1,184
  • 1
  • 11
  • 19
  • This logic stops after the first row that does not have the check-box checked. Is that the desired behavior? – cdkMoose May 18 '15 at 15:37
  • Richardo, I tried the above code, it gets values from the entire column when I select one row. I want to get value/s from row/s selected. CdkMoose, yes or is there a better way. – user3613720 May 18 '15 at 15:46
0

Having a check-box column in your grid will not change the internal status of any of the rows, so you will need to iterate over them all yourself to evaluate. This should do the trick, although you will need to provide the correct column index for the checkbox column in checkBoxColumnIndex

int checkBoxColumnIndex = nnn; // 0 based index of checkboxcolumn
private void button1_Click(object sender, EventArgs e)
{
    List<string> checkedItems = new List<string>();

    dataGridView1.ClearSelection();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell checkBox= row.Cells[checkBoxColumnIndex] as DataGridViewCheckBoxCell;

        if (Convert.ToBoolean(checkBox.Value) == true)
        {                    
             checkedItems.Add(row.Cells[1].Value.ToString());
        }
    }
    if (checkItems.Count > 0)
        MessageBox.Show("Row Content -\r\n" + String.Join("\r\n", checkedItems));
    else
        MessageBox.Show("No row is selected!");
}

Admittedly, the List<string> is a heavy construct if all you want to do is print the list, but it would be useful if you need to do further processing on all of the checked values.

cdkMoose
  • 1,646
  • 19
  • 21
0

Here is a version of your code that may be what you want..but it is hard to tell because you are talking about selected rows but are clearing the selection before processing the rows!

That makes no sense.. maybe you mean the checked rows? OK, you do, so here you go:

  private void button1_Click(object sender, EventArgs e)
  {
      StringBuilder ln = new StringBuilder();
      dataGridView1.ClearSelection();
      foreach (DataGridViewRow row in dataGridView1.Rows)
      {
          if (((bool?)row.Cells[0].Value) == true)
          {
               ln.Append(row.Cells[1].FormattedValue);
          }
      }
      if (ln.Length <= 0) MessageBox.Show("No rows are checked!");
      else MessageBox.Show("Rows content: " + ln);
 }
TaW
  • 53,122
  • 8
  • 69
  • 111
  • TaW, how can you make this to get values from checked rows? Currently, it get values if you check and highlight rows using control+left click.... – user3613720 May 18 '15 at 19:03