如何获取关于gridview的行数据绑定事件的命令参数 [英] How to get the Command Argument on Row Data bound Event of gridview

查看:43
本文介绍了如何获取关于gridview的行数据绑定事件的命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样将值绑定到gridview

i am binding the values to gridview like this

<asp:GridView ID="grdViewAttachment_Client" runat="server" Width="615px" AutoGenerateColumns="False"  GridLines="None" CssClass="grid-view" OnRowCommand="grdViewAttachment_Client_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Attachment" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnAttachments" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Eval("AttachmentId") %>' ></asp:LinkButton>
</ItemTemplate>
<HeaderStyle/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attachment" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<%--<asp:LinkButton ID="lnkbtnAttachments" runat="server" Text='<%#Eval("AttachmentName") %>' CommandName='<%#Eval("AttachmentName")%>' CommandArgument='<%#Eval("AttachmentId") %>'></asp:LinkButton>--%>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("AttachmentName") %>' CommandName='<%#Eval("AttachmentName")%>' ></asp:LinkButton>
</ItemTemplate>
<HeaderStyle/>
</asp:TemplateField>
<asp:TemplateField HeaderText="AssignedTo" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<asp:Label ID="lblAssignedTo" Text='<%#Eval("AssignedTo") %>' runat="server" CssClass="body-text"></asp:Label>
</ItemTemplate>
<HeaderStyle />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreationDate" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<asp:Label ID="lblCreationDate" Text='<%#Eval("CreationDate") %>' runat="server" CssClass="body-text"></asp:Label>
 </ItemTemplate>
 <HeaderStyle />
 </asp:TemplateField>
 </Columns>
 </asp:GridView>

在行命令事件中,我编写如下代码

In my row command event i write as follows code

protected void grdViewAttachment_Client_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SqlConnection m_Conn = new SqlConnection(Utilities.ConnectionString());
                SqlCommand m_oCmd;
        int iStID = int.Parse(e.CommandArgument.ToString());
        int pk = 0;
        int.TryParse(e.CommandArgument as string, out pk);
        string strStoreProcName = null;
        DataSet oDS1 = new DataSet();
        if (e.CommandName == "Delete")
        {
            strStoreProcName = StoredProcNames.Attachments_uspDeleteAttachs;
            m_oCmd = new SqlCommand(strStoreProcName, m_Conn);
            m_oCmd.CommandType = CommandType.StoredProcedure;
            m_oCmd.Parameters.Add("@AttachmentId", SqlDbType.Int).Value = Convert.ToInt32(e.CommandArgument.ToString());
            m_Conn.Open();
            m_oCmd.ExecuteNonQuery();
            AttachmentDetails();
        }
        string fname = e.CommandName.ToString();

    }

但是我没有得到价值,我得到了例外,因为Input string was not in correct format谁能帮助我

But i am not getting the value i am getting the exception as Input string was not in correct format can any one help me

推荐答案

es @穆罕默德·阿赫塔尔(Muhammad Akhtar)是正确的.应该像

es @Muhammad Akhtar was right .It should be like

int iStID = Convert.ToInt32(gridName.DataKeys[Convert.ToInt32(e.CommandArgument.ToString())].Value);

在数据网格中指定Datakey属性.您可以像上面

Specify Datakey property in your data grid . and you can access the data key value of the current row as above

在您的语句中添加数据键名称

add datakey name to your statement

ID_COLUMN应该是要绑定到gridview的数据表中的一列

ID_COLUMN should be a column from data table which you are binding to gridview

将此代码写入创建的行

protected void grdViewAttachment_Client_RowCreated(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {   
        LinkButton lnkbtnAttachments=(LinkButton)e.Row.FindControl("lnkbtnAttachments");       
        lnkbtnAttachments.CommandArgument = e.Row.RowIndex.ToString();

        // similarly write for other controls
    }
}

这篇关于如何获取关于gridview的行数据绑定事件的命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆