Datagrid Button column

laxman

Freshman
Joined
Jun 14, 2008
Messages
28
Dear All,

I am developing a web application using asp.net with c#

in this i am using datagrid to show the information which should also have an option to update the data this i am doing by providing the button column and remainging columns i am using are item template for this i have created the event handler to fill the data in to another form while executing i am getting null values
here i am giving my code:
.aspx page code
[highlight=asp]
<TABLE border="0" cellSpacing="0" cellPadding="0">
<TR>
<TD align="center">
<asp:datagrid id="RunTimeGrid" runat="server" OnItemCommand="ItemCommand" BorderWidth="1px" PagerStyle-Font-Bold="True" BorderStyle="None"
BorderColor="White" AutoGenerateColumns="False" CellPadding="3" BackColor="White" OnSelectedIndexChanged="RunTimeGrid_SelectedIndexChanged">
<ItemStyle BorderColor="#e2e5e7" BackColor="#e2e5e7"></ItemStyle>
<HeaderStyle Font-Name="Tahoma" Font-Size="10" Font-Bold="True" ForeColor="White" BorderColor="Black"
BackColor=#2596AA></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Sl.No.">
<ItemTemplate>
<SPAN>
<%# (Container.DataSetIndex)+1 %>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="transaction_id">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "transaction_id")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="mobile_no">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "mobile_no")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Transaction_date">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "Transaction_date")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Transaction_Time">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "Transaction_Time")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="vendorTransactionId">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "vendorTransactionId")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="STATUS">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "STATUS")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="AMOUNT">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "Amount")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "Description")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Mode">
<ItemTemplate>
<SPAN>
<%# DataBinder.Eval(Container.DataItem, "Mode")%>
</SPAN>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="EDIT" ButtonType="PushButton" HeaderText="Clicktoaccept" CommandName="Edit">
<ItemStyle ForeColor="Black"></ItemStyle>
</asp:ButtonColumn>

</Columns>
<PagerStyle Font-Bold="True"></PagerStyle>
</asp:datagrid></TD>
</TR>
</TABLE>


[/highlight]
.aspx.cs code
C#:
 protected void ItemCommand(Object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {

            FillTheData(e.Item.Cells[2].Text);
            //e.Item.c
            string s = e.Item.Cells[2].Text;
            Session["Username"] = e.Item.Cells[0].Text;
            Session["as"] = e.Item.Cells[1].Text;
            Session["asd"] = e.Item.Cells[2].Text;
            Session["abcd"] = e.Item.Cells[3].Text;
            Session["abcde"] = e.Item.Cells[4].Text;
            Session["abcdef"] = e.Item.Cells[5].Text;
            Session["lax"] = e.Item.Cells[6].Text;
            Session["man"] = e.Item.Cells[7].Text;
            //LabError.Text="Sorry It is already Assigned";
            Response.Redirect("Update.aspx");
        }
please guide me what is my coding problem i am developing it in visual studio 2005
 
Last edited by a moderator:
I don’t think your cell will contain any text because you’re using databinding. Rather it’ll contain a databoundcontrol. I don’t really know much about how you’d get that value and how it’s persisted in a postback.
You could use the item index and then finding the values from the original data source. This will mean less performance as you’ll need to re-fetch the data in the postback.

Code:
        protected void itemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                var index = e.Item.ItemIndex;
                var dataSource = DataSource();
                var data = dataSource[index];
                Session["Username"] = data.Username;
                //etc
            }
        }

Or you could pass the selected row are a parameter to your “Update.aspx” page and let that page find the data from your datasource.

Code:
        protected void itemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                var index = e.Item.ItemIndex;
                var url = string.Format("Update.aspx?rowIndex={0}", index);
                Response.Redirect(url);
            }
        }
 
Back
Top