Reputation: 1512
I'm wondering if it's possible to check a GridViewRow's BackColor during the GridView_OnRowEditing event.
My current code only returns [Name=0, ARGB=(0, 0, 0, 0)] when I try to get the row.BackColor
Here is my code:
protected void GridView_OnRowEditing(object sender, GridViewEditEventArgs e)
{
var row = GridView1.SelectedRowStyle;
if (row.BackColor == Color.White)
{
errorLabel.Text = "ERROR - cannot edit this row";
}
else
{
//Different code
}
}
Thanks in advance.
Edit: Is there an alternative to DataGridViewRow.Tag with asp GridViewRow?
Upvotes: 2
Views: 402
Reputation: 1512
I was able to find my own solution by doing this:
if (GridView1.Rows[e.NewEditIndex].BackColor != Color.SeaGreen &&
GridView1.Rows[e.NewEditIndex].BackColor != Color.IndianRed)
{
e.Cancel = true;
errorLabel.Text = "Please scan roll before updating QtyRun";
}
else
{
//do something else
}
@RichardDias idea would've been an okay, but asp GridViewRow does not allow for the Tag property (DataGridViewRow does allow for the Tag property).
Another reason I would not have used his method is because I'm already taking time to highlight specific rows (which needs to be done, no alternative) in my gridview. To add another method of 'confirmation' seems inefficient.
By using my method, I was able to utilize my initial 'confirmation by row color' method to cancel the Editing event. Regardless, thank you @RichardDias for your help.
Upvotes: 2
Reputation: 230
Try do something like this:
public void ScanRows()
{
foreach (DataGridViewRow row in GridView1.Rows)
{
row.DefaultCellStyle.BackColor = Color.Green;
//Indicate that this row already was scanned
row.Tag = true;
}
}
protected void GridView_OnRowEditing(object sender, GridViewEditEventArgs e)
{
var row = GridView1.SelectedRow;
if (!((bool)(row.Tag ?? false)))
{
errorLabel.Text = "ERROR - cannot edit this row";
}
else
{
//Different code
}
}
Upvotes: 1