Gridview Row - 值和列名称

时间:2012-06-11 11:53:06

标签: asp.net gridview

我想获取我点击的列的名称以及此点击行的值。

我找到了很好的例子,但我无法得到我想要的数据。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onClick"] = "location.href='view.aspx?id_lekarza=" + DataBinder.Eval(e.Row.DataItem, "Id_lekarza") + " &klikniete=" + "sss" + " '";
            }
        }

e.Row.RowIndex一直是0?

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onclick", "javascript:alert('" + e.Row.RowIndex + "');");
                e.Row.Style.Add("cursor", "pointer");
            }
        }

1 个答案:

答案 0 :(得分:1)

在GridView中,您可以添加如下模板字段:

<asp:TemplateField ItemStyle-Width="44px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
                            HeaderStyle-Height="40px" ShowHeader="false">
                            <ItemTemplate>
                                <asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/Images/btnSelect.png"
                                    CommandName="Select" CommandArgument='<%# Eval("ID") %>'/></ItemTemplate>
                            <HeaderStyle Height="40px"></HeaderStyle>
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="44px"></ItemStyle>
</asp:TemplateField>

在你的C#代码隐藏中你应该创建Gridview的RowCommand事件,并访问该行的CommandArgument,如下所示:

  protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
    {
     //You get the ID of that specific row that you have selected :
     string ID=e.CommandArgument.ToString();
     //Here you write your code !          For example a select query to get that row from database by the ID you have  gained in above line of code
    }
}