2

In my grid I have check boxes and I have a button out side the grid. When I click on the button I want to get the count of only selected rows with check boxes in the grid. How can we do that?

In my grid view dynamically create check box and another columns, I have site id column, I need to get the site id what are the check box checked.

I use this coding

int count = dataGridView1.Rows.Count;
for (int i = 0; i < count; i++)
{
   int j = dataGridView1.SelectedCells[i].RowIndex;
   clsSaveStatic.SiteID = int.Parse(dataGridView1.Rows[j].Cells[1].Value.ToString();
}

It returns finally Which check box is checked . But my requirement is all the checked values i need one by one.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
user990947
  • 21
  • 3
  • 5

4 Answers4

1

Dude.

Heres is what I did:

Set the CellValueChanged event from the Grid to check for "true/false" values.

            if (DataGridView.Rows.Count > 0)
            {
                if ((bool)DataGridView.Rows[e.RowIndex].Cells[0].Value)
                { VarToControlCount++; }
                else if ((bool)DataGridView.Rows[e.RowIndex].Cells[0].Value == false)
                { VarToControlCount--; }

                Label.Text = "Selecionados: " + VarToControlCount.ToString("00");

This will identity if the value from the checkbox is true everytime it's changed.

THEN...

To "Refresh" the count, simply set the focus on another cell. Well, the only way I figured out was to send a "TAB" when the user checks something in the DGV.

In the "CellClick" event from the DGV you're gonna do like this:

SendKeys.Send("{TAB}");

DONE. That was the only way I got to force a refresh. Simple and clean, works great though.

Check it out how it looks:

Morilon
  • 11
  • 1
1

ok now I put some code example I've used before to do same job it part of my code may be useful for you

private List<int>() GetSiteId()
{
    var listIds=new List<int>();

    for (int i = 0; i < grid1.RowCount - 1; i++)
    {
        if (grid1.Rows[i].Cells["SelectCol"].Value != null)
        {
            bool value = Convert.ToBoolean(grid1.Rows[i].Cells["SelectCol"].Value);
            if (value)
            {
                 listId.Add(Convert.ToInt32(grid1.Rows[i].Cells["SiteID"].Value));
            }
        }
    }
    return listId;
}

so if value=true its checked

glosrob
  • 6,631
  • 4
  • 43
  • 73
DeveloperX
  • 4,633
  • 17
  • 22
  • This Example is not working for my requirement. So pls give some another example. – user990947 Nov 03 '11 at 05:33
  • It loops through the rows and gets the checked values,what do you realy want? what do you mean one by one? – DeveloperX Nov 03 '11 at 05:59
  • In my Grid View Dynamically bind the values I have Site Id columns i check the multiple check box in the form what are check box selected that all the SiteId value i need – user990947 Nov 03 '11 at 07:21
1

you will have to loop through the rows of the DataGridView and check for the checkbox cell

foreach (DataGridViewRow item in dataGridView1.Rows)
{
       //if checkbox is checked
       if(Convert.ToBoolean((item.Cells["Chkboxcolname"] as DataGridViewCheckBoxCell).FormattedValue))
              // here you can access values based on column names
              //item.Cells["Idcolumn"].FormattedValue
}

Now if the cell is checked then the value of the cell when evaluated as a boolean will be true

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

You can refer following links

http://www.codeproject.com/Answers/135987/Getting-Checkbox-value-from-DataGridView#answer4 http://stackoverflow.com/questions/1237829/datagridview-checkbox-column-value-and-functionality

AjayR
  • 4,169
  • 4
  • 44
  • 78