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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing gridview problem with rowdatabound event

Status
Not open for further replies.

mrp9090

Programmer
May 22, 2006
71
GB
I have a couple of columns in my gridview that I am adding formatting to in the RowDataBound event. But when I want to edit the rows when the RowDataBound event gets called, I get an error. I'm thinking that I need some kind of statement where I check whether the grid is in editing mode before I run the code. Here is my code :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label ForecastType = (Label)e.Row.FindControl("lblForecastType");

switch (ForecastType.Text)
{
case "Analysis":
e.Row.Cells[2].BackColor = System.Drawing.Color.Cyan;
break;

case "Call Centre":
e.Row.Cells[2].BackColor = System.Drawing.Color.Lime;
break;

case "Data":
e.Row.Cells[2].BackColor = System.Drawing.Color.Yellow;
break;

case "Data/Analysis":
e.Row.Cells[2].BackColor = System.Drawing.Color.Magenta;
break;
}

Label Probability = (Label)e.Row.FindControl("lblProbability");

switch (Probability.Text)
{
case "0.25":
e.Row.Cells[4].Text = "A (25%)";
e.Row.Cells[4].BackColor = System.Drawing.Color.Coral;
break;

case "0.5":
e.Row.Cells[4].Text = "B (50%)";
e.Row.Cells[4].BackColor = System.Drawing.Color.OrangeRed;
break;

case "0.75":
e.Row.Cells[4].Text = "C (75%)";
e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
break;

case "0.751":
e.Row.Cells[4].Text = "Proforma (75%)";
e.Row.Cells[4].BackColor = System.Drawing.Color.Maroon;
break;

case "1":
e.Row.Cells[4].Text = "In (100%)";
e.Row.Cells[4].BackColor = System.Drawing.Color.White;
break;
}

LinkButton EditButton = (LinkButton)e.Row.Cells[8].Controls[0];
EditButton.ForeColor = System.Drawing.Color.Blue;

LinkButton DeleteButton = (LinkButton)e.Row.FindControl("DeleteButton");
DeleteButton.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete forecast number " +
DataBinder.Eval(e.Row.DataItem, "ForecastKey") + "?')");

DeleteButton.ForeColor = System.Drawing.Color.Blue;
}
}

Any assistance would be really appreciated!


Cheers,

Mike
 
I get an error
Would you like to elaborate?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
The error is 'Object reference not set to an instance of an object' when the RowDataBound is being executed after clicking on the Edit button of the GridView.
 
And on which line does it occur?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
This line :

switch (ForecastType.Text)
 
In that case, your FindControl method isn't finding a Control named "lblForecastType".


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
But this error only occurs in edit mode, and even then it goes through this code for many of the rows fine before producing this error.
 
Yes, that's because it will go through all the rows that do have the control but when it comes to the edit row it won't exist. Check the RowState and only find the control when it isn't in edit mode.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
This is the line I was looking for :

if (e.Row.RowState != DataControlRowState.Edit)

Is it useful adding DataControlRowState.Insert to this as well, since I would think you would get the same problem when inserting a row?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top