Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get value of DataGridViewCheckBox

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hello
I have a datagridview which containts a checkbox column. I need to konw what the value is of the checkbox in order to enable or disable a button. it seems like any events I try to capture this with is too early.

id offer code, but lets just say I scrapped it all

Any help is appreciated.

RalphTrent
 
I use this method to get a button in a gridview, should be much the same for a checkbox, let see...

Code:
<script runat="server">
  void chkBox1_CheckChanged(object sender, EventArgs e)
  {
    CheckBox cb = (CheckBox)sender;
    GridViewRow grdRow = (GridViewRow)cb.Parent.Parent;
    //get the row that was clicked
    int index = Convert.ToInt32(grdRow.RowIndex);
    //find the control by name and index
    CheckBox myCheckBox = (CheckBox)grdProducts.Rows[index].FindControl("<CONTROL NAME>");
        }
</script>

Hope this helps,

Patrick
 
Hi Patrick, thanks for the reply. I should have mentioned that this was for a WinApp, not a Web App. But from looking at your code, it looks like you are referring to a CheckBox control and putting an event wrapper around it. I thought that might work regardless of interface since i do have a System.Windows.Forms.DataGridViewCheckBoxColumn() controls on the page that is used to create the checkbox feature, however, there are not events other then dispose for this control.

I think I need to react to an event for the datagridview itself.

Any idea's?

Thanks again.

RalphTrent
 
I figured it out:

Code:
private void dgvResults_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
	if (dgvResults.CurrentCell.ColumnIndex == 0)[green]//My checkbox is column 0[/green]

        {
		dgvResults.CommitEdit(DataGridViewDataErrorContexts.Commit);
	}
}

private void dgvResults_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
	if (e.ColumnIndex == 0) [green]//My checkbox is column 0[/green]
        {
        	setDeleteFolderStatus();
	}
}

private System.Collections.Generic.List<DataGridViewRow> getCheckedRows()
{
	System.Collections.Generic.List<DataGridViewRow> gdgvrReturn = new System.Collections.Generic.List<DataGridViewRow>();
        foreach (DataGridViewRow dgvr in dgvResults.Rows)
        {
        	if (dgvr.Cells[0].Value != null)
                {
			if (((DataGridViewCheckBoxCell)dgvr.Cells[0]).Value.Equals(true))
			{
				gdgvrReturn.Add(dgvr);
			}
		}
	}
	return gdgvrReturn;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top