Saturday, April 25, 2009

Change GridView row values using RowUpdating event


The contents of this post has been moved to here. Please visit the link to view the contents.

Sorry for the inconvenience.

Thanks!

5 comments:

ranjith said...

Nice Code :). The concept is neatly explained.

NobleMule said...

Fantastic code, really helped me with a DDL in a gridview issue. One question though.

I have 4 DDL's in a gridview. The first one is updating (mostly thanks to your code) but I can't get the other three to update. I'm a fairly new to C# and have never used the DictionaryEntry before. I naively tried just adding more if's (one for each ddl) within the foreach loop but that didn't work. Any help would be greatly appreciated.

Abhilash said...

Hi NobleMule, I'd written a new post for your question. Hope it'll be be solving your problem. Please let me know, whether it's solved or not.

http://dodgethecode.blogspot.com/2009/08/changing-multiple-gridview-row-values.html

NobleMule said...

Thanks so much for the reply and follow-up post. Unfortunately, it hasn't solved my problem. My code is as follows:

protected void NewRecordsGridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
foreach (DictionaryEntry entry in e.NewValues)
{
DropDownList ddl;
if (entry.Key.ToString() == "Direction")
{
ddl = (DropDownList)NewRecordsGridview.Rows[e.RowIndex].Cells[7].FindControl("DirectionGVDDL");
e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
}
if (entry.Key.ToString() == "Outcome")
{
ddl = (DropDownList)NewRecordsGridview.Rows[e.RowIndex].Cells[6].FindControl("OutcomeGVDDL");
e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
}
if (entry.Key.ToString() == "NextRunMonth")
{
ddl = (DropDownList)NewRecordsGridview.Rows[e.RowIndex].Cells[8].FindControl("NRMGVDDL");
e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
}
}
}

The GridView I'm using has over 10 columns. However the foreach loop only cycles 5 times. On the third cycle, the first if statement is triggered but only the first one.

Once again, many many thanks for the follow-up post. I didn't expect a response so soon, let alone a response so thorough.

Abhilash said...

Hi,

I didnt find anything wrong with your code.

>However the foreach loop only cycles 5 times.
> On the third cycle, the first if statement is
> triggered but only the first one.

This portion is not clear to me.

I suggest you to
(a) debug the code, by inserting Breakpoint (F9). That gives you a better control over your code.

(b) Cross-check the column names of the table used to compare with entry.Key.Tostring() (in your code).

Hope this helps.
Thanks.

 
Best viewed in Internet Explorer 8.