ASP.NET 2.0 GridView Tips - Keyword like "Update" and BoundField Visible=False

1) Tip #1

Assuming you have this code:

<asp:ButtonField Text="Edit" HeaderText="Action" CommandName="Update" />

Then you have an event to get that command and process the necessary update to your field as below:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

                if (e.CommandName == "Update")

                {

                                // process your update codes here

                }

}

You will be surprised that you will not get it to work. It will prompt you that you do not handle the RowUpdating/RowUpdated event as well.

So the tip here is, does not use keyword like Update. Named it something else likes this “UpdateModuleA” or something meaningful.

2) Tip #2

Assuming you have this code:

<asp:BoundField DataField="Remarks" Visible="false" HeaderText="Remarks" SortExpression="ExpRemarks">

 

Assuming you have a gridview that will only show 5 columns of data bound values. You will call a stored procedure which actually passes 6 columns of values. One of them is actually an invisible column, just like above.

 

When you try to get values from the GridView by passing the code as below:

 

grvExpenditure.Rows[index].Cells[ColumnIndex].Text

 

You will not find the value you want. But when you try to set the column to visible=true, you can see the value.

Published Monday, November 06, 2006 9:25 AM by chuawenching