5

I have a datagridview that contains list of subjects populated from Subject table from database.Columns include

  • Select(checkbox),
  • SubjectId,
  • SubjectName,
  • SubjectGroup.

Now I want if a user Selects on any of the desired rows, the corresponding SubjectId's should be added to a List. I have made and inserted into the desired table in the database. The problem is that the new column of checkboxes I have added to this datagridview is not being detected.

My code is:

    foreach (DataGridViewRow row in gvSubjectsOpted.Rows)
        {

            if (Convert.ToBoolean(gvSubjectsOpted.SelectedRows[0].Cells["SelectId"].Value=true))
            {
                olist.Add(gvSubjectsOpted.SelectedRows[0].Cells["SubjectId"].Value.ToString());
            }
        }
Muhammad Suleman
  • 53
  • 1
  • 1
  • 6

5 Answers5

11

Late to the party. I had the same issue with trying to get the checkbox column by name, use the index instead. Here is a linq example assuming the checkbox is column 0 and the stored values for TrueValue and FalseVale are true and false respectively.

        var checkedRows = from DataGridViewRow r in gvSubjectsOpted.Rows
            where Convert.ToBoolean(r.Cells[0].Value) == true
            select r;

        foreach (var row in checkedRows)
        {
            olist.Add(row.Cells["SubjectId"].Value.ToString());
        }
Bleeped
  • 469
  • 7
  • 16
2

I realise this is an old post but I came across it and didn't think it was really answered in an efficient way so I thought I would add my method.

I have a similar block in my windows app. I read the values from the grid when the user clicks a button, and I want to know which rows they checked. As the checkboxes are in Cell 0 and the data I want is in Cell 1, I use the following code. Note the cast: it is important as it allows us the use the Where clause and therefore just a single line of code to get the collection of data. I could use the name of the cells instead of magic index numbers but then it would not fit your app so I put numbers instead (you should use names)

    var checkedRows = dataGridView
        .Rows
        .Cast<DataGridViewRow>()
        .Where(x => x.Cells[0].Value.ToString() == "1")
        .Select(x => x.Cells[1]);

Note that this will give you an IEnumerable of type DataGridViewCell. If you want you can either add something like .Value.ToString() to the select or do this when you use your collection.

David Bridge
  • 848
  • 9
  • 14
1

You question is similar to another SO question.

Check the answer of this Datagridview checkboxcolumn value and functionality.

Community
  • 1
  • 1
Junaith
  • 3,298
  • 24
  • 34
0

Try this

foreach(GridViewRow r in gvSubjectsOpted.Rows)
{
    GridViewCheckBoxColumn c = r.cells[0].Controls[0] as GridViewCheckBoxColumn;
    if(c.Checked)
    {
      //Do something.
    }
}
Hitesh
  • 3,449
  • 8
  • 39
  • 57
  • @Hitech Salia, it gives an error of FindControl...also SubjectId is my column name which I want to get in case it's row is checked by the user. – Muhammad Suleman Feb 25 '14 at 11:48
0

private void button1_Click(object sender, EventArgs e)

    {   
        string subjId;   
        List<string> lines = new List<string>();   
        for (int i = 0; i < gvSubjectsList.Rows.Count; i++)   
        {   
       bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);    
            if (Ischecked == true)   
            {   
                subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();   
                lines.Add(subjId);   
            }   
    }
    comboBox1.DataSource = lines;     

    }  

//the most important thing is to set 'true' and 'false' values against newly added checkboxcolumn instead of '0' and '1'...that is,

CBColumn.FalseValue = "false";
CBColumn.TrueValue = "true";

Muhammad Suleman
  • 53
  • 1
  • 1
  • 6