0
votes

I have a DataGridView which has a checkbox column. What im trying to do is, from a button click, update those rows where the checkbox is checked by getting a certain column data from those rows (id) and send it to the update sql command I have. First I populate the datagridview with a datatable and after that I create a checkbox column with readonly property as false so that I can click it. Then from the onclick event from the button, do something like this:

private void AnularBoton_Click(object sender, EventArgs e)
    {
        foreach datagridview row in datagrid {
          if a certain column (checkbox) from that row is checked
          then get the id from that row (id is the first column)
        }
    }

I will be saving those ids in an array and update them all at once.

3
So why your code doesn't work? - Fabio
I guess im very tired to find a way, nevertheless I did an approach similar to the answers List<int> segurosID = new List<int>(); foreach (DataGridViewRow dc in SegurosDataGridView.Rows) { if (dc.Cells["Anular"].Value != null) { if ((Boolean)dc.Cells["Anular"].Value == true) { segurosID.Add(Convert.ToInt32(dc.Cells["Seguro"].Value)); } } } - Popplar

3 Answers

1
votes

I have Read your question you want try the below code the below code is check the checkbox button column value is check then that rows id get...try the below code

foreach(DataGridViewRow dgr in yourdatagridviewname.Rows)
{
    if(dgr.Cells["yourcheckboxcolumnName"].Value==True)
    {
        var id=Convert.ToInt32(dgr.Cells["youridfieldName"].Value.ToString())
    }
}
1
votes

Add a Boolean column to the datatable, then do:

foreach(DataRow ro in YourDataTableNotYourDataGridView.Rows){
  if((bool)ro["BOOLEAN COLUMN NAME HERE"]){
    //do something
    int id = (int)ro["id"];

    //but if you're manipulating the data, just do it directly, don't collect the id
    ro["name"] = "John";

    //the only thing you can't do, is delete the row from the table, because your enumerating the table
    //if you want to do that, use a regular for loop (with an index int) in reverse order
  }
}

The user will tick the relevant rows using the DGV, causing the Boolean to become set in the data model, and then you use that

You're focusing on the datagridview as if it's something that contains data* and should be programmatically manipulated and interacted with; it shouldn't. The DGV is purely a tool for the user to use to see and manipulate the data model. Programmatically you should access and manipulate the model of data, not the view/controller

*you said "first I populate the DGV with data" - this isn't the correct way to regard it. The DGV is a View of data that lives elsewhere, it doesn't get populated with any data itself directly, nor should it. A core concept of MVC theory is that the model, view and controller concerns remain separated. It happens for practicality that V and C are often done by the same thing, but M remains distinct

In addition, your life will get a lot easier if you use strongly typed datasets for this, much less casting and column-name-in-a-string access:

foreach(TypedDataRow ro in typedDataTable){
  if(ro.BoolColumnName)
    //do something
}

Things get easier and more compact when you involve LINQ too:

List<int> segurosId = datatable.Where(r => r.BoolColumn).Select(r => r.id).ToList();

That line of code does the same as your;

List<int> segurosID = new List<int>(); foreach (DataGridViewRow dc in SegurosDataGridView.Rows) { if (dc.Cells["Anular"].Value != null) { if ((Boolean)dc.Cells["Anular"].Value == true) { segurosID.Add(Convert.ToInt32(dc.Cells["Seguro"].Value)); } } }

Final point of note; just iterate the datatable and change the data directly; don't get into some circuitous method of iterating the datatable, looking for booleans, pulling out IDs, using those IDs to retrieve each row, changing the row.. You might have to do that if you're making the mistake of rummaging through the datagridview as if it were a store of data, but the key concept of this answer is to get you into MVC and understanding the separation, programmatically changing the data directly and leaving the user to non-programmatically change the data via the gridview. As an extreme analogy, if you were writing a program that updated a database, you would just update the database, you wouldn't install Sql Server management Studio on the machine, then use SendKeys to control it and have it update the database. In the same way you shouldnt set about manipulating data in a datatable by programmatically interacting with the grid view to which that data is connected

0
votes

Try the below code that will get your checkbox column value and stored in array:

   foreach(DataGridViewRow dgr in DataGridview.Rows)
    {
       if(dgr .Cells["checkbox"].Value)==True)
       {
          int id=dgr.Cells["id"].Value);
        }              
    }

Thank you.