Reputation: 79
I have a checkbox inside a datagridview that allow user to calculate the total amt of the selected row. But I have to make authorization validation that ANY user can tick the rows BUT only MASTER can untick the rows.
So I make the calculation on CellClick
event.
Here the problem I face is when i click on the combobox, it still will untick no matter how i force it to tick back.
if (e.ColumnIndex == gv.Columns["ColumnMark"].Index)
{
if (gv.Rows[e.RowIndex].Cells["ColumnMark"].Value.ToString() == "1")
{
if (authorized == "TRUE")
{
gv.Rows[e.RowIndex].Cells["ColumnMark"].Value = 0;
DisplayItemTotalAmount();
}
else
{
gv.Rows[e.RowIndex].Cells["ColumnMark"].Value = 1;
DisplayItemTotalAmount();
}
}
else
{
gv.Rows[e.RowIndex].Cells["ColumnMark"].Value = 1;
DisplayItemTotalAmount();
}
}
I had already try on CellBegin
event and CellValueChanged
event but i will not validated or calculate when i click on the same checkbox second times.
I think there should be have one Event Handler
for this. Because the CellClick
work but the mark is still perform untick.
Hope anyone can help me and sorry for my bad english.
Upvotes: 1
Views: 1442
Reputation: 5454
Your code isn't working because ACheckBoxCell.Value.ToString()
when not null, will always be "true"
or "false"
, never "1"
or "0"
. Change this logic and problem solved.
As an extra: fixing this, you'll notice your cell checked state doesn't visually update until you leave the cell. There's a simple fix for that too. See below:
private void gv_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == gv.Columns["ColumnMark"].Index)
{
if ((bool)gv.Rows[e.RowIndex].Cells["ColumnMark"].Value)
{
if (authorized == "TRUE")
{
gv.Rows[e.RowIndex].Cells["ColumnMark"].Value = 0;
}
else
{
gv.Rows[e.RowIndex].Cells["ColumnMark"].Value = 1;
}
}
else
{
gv.Rows[e.RowIndex].Cells["ColumnMark"].Value = 1;
}
gv.RefreshEdit();
gv.NotifyCurrentCellDirty(true);
DisplayItemTotalAmount();
}
}
private void gv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
gv.RefreshEdit();
}
Upvotes: 1
Reputation: 1647
I assumed you are working with a datagridview since the tag is for C# only but Asp and c#. So may be you can try updating the currentcell
value.
if (gv.CurrentCell.Value == 1 && authorized == "TRUE")
{
gv.CurrentCell.Value.ToString() == 0;
DisplayItemTotalAmount();
}
Upvotes: 0