Posted: 1/22/2009
I have a GridView that I am binding to a List<T>. Whenever I try to access the data in the RowCommand event, everything is null.
I read the "How to: Respond to Button Events in a GridView Control" article and this is what it said to do:
// Retrieve the row index stored in the // CommandArgument property.int index = Convert.ToInt32(e.CommandArgument);// Retrieve the row that contains the button // from the Rows collection.GridViewRow row = GridView1.Rows[index];
But when I do that and check row.DataItem, or GridView1.DataSource both are null. How can I get to and update the data?
It should work, may be there is something else missing. Can you post your entire code?
Also, do visit this post too, where they show another way fo retrieving data:
http://forums.asp.net/t/1297328.aspx
//Find Row GridViewRow _rw = (GridViewRow)(((Button)e.CommandSource).NamingContainer); //Refer Controls inside that rowLabel _lb = (Label)_rw.FindControl("lblMsc");
Posted: 1/27/2009
Hi Vivek,
Sorry for late reply. i was out of town.
The HTML is as below,
<asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 0px; position: absolute; top: 0px" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField HeaderText="Click"> <ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Click" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
And the codebehind is,
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
List<string> list = new List<string>(); list.Add("a"); list.Add("b"); list.Add("c"); list.Add("d"); list.Add("e");
if (!Page.IsPostBack) { GridView1.DataSource = list; GridView1.DataBind(); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Click") { // Retrieve the row index stored in the // CommandArgument property. int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button // from the Rows collection. GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart. }
}}
Now in the GridView1_RowCommand event handler if you check the DataItem property of the GridView row object you will get null. Same thing is true for GridView1.DataSource.
You can debugg through the code and check it out.
Posted: 1/28/2009
Joydeep,
I ran you code, and I am able to access the row's data using syntax like:
row.Cells[1].Text
Note that you cannot use gridview1.datasource or dataitme properties because this is a postback, and during postback you have loaded gridviews state from the ViewState not by using gridview1.DataSource = source.
Hope this helps,
Vivek
Vivek,
ya...row.Cells[1].Text is working fine.
But why row.DataItem is not working?Is it a bug or what?
Thanks man
Posted: 1/29/2009
You can use that property only when the GridView is fileld using DataSoruce and DataMember properties. but during postback, the gridview datasource is not set, instead the runtime loads the data in it from the ViewState. Hence you can't really access the data using DataItem.
HTH,