且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ASP.net,C#在GridView中选择行值

更新时间:2023-12-06 15:39:58

您好,



请查看以下示例代码:



HTML



Hi,

Please check below sample code:

HTML

<asp:GridView runat="server" ID="gvProjects" AutoGenerateColumns="false" OnRowCommand="gvProjects_RowCommand">
        <Columns>
            <asp:BoundField HeaderText="Projcet Name" DataField="ProjectName" />
            <asp:ButtonField ButtonType="Link" CommandName="Select" Text="Select" />
        </Columns>
    </asp:GridView>





代码隐藏





Code behind

protected void gvProjects_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // If multiple buttons are used in a GridView control, use the
            // CommandName property to determine which button was clicked.
            if (e.CommandName == "Select")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button clicked 
                // by the user from the Rows collection.
                GridViewRow row = gvProjects.Rows[index];
                
            }
        }







问候,

Jamil




Regards,
Jamil


使用 GridView.SelectedRow属性 [ ^ ]。


嗨!

我试过这段代码,愿它能帮助你 b $ b

ASP代码

Hi!
I Have tried this code,May it Will help you

ASP Code
<asp:gridview id="gvdet" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
    BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
    CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" 
    GridLines="Horizontal" Height="271px" Width="346px"
    OnRowCommand="gvdet_sub" >
        <columns>
            <asp:boundfield datafield="id" headertext="id" readonly="True">
                SortExpression="id" />
            <asp:boundfield datafield="name" headertext="name" sortexpression="name" />
            <asp:boundfield datafield="surname" headertext="surname">
                SortExpression="surname" />
            <asp:boundfield datafield="address" headertext="address">
                SortExpression="address" />
            <asp:buttonfield headertext="Value" buttontype="Button" runat="server">
                CommandName ="cmdname" Text="Show" />
            
        </asp:buttonfield></asp:boundfield></asp:boundfield></asp:boundfield></columns>
        <footerstyle backcolor="White" forecolor="#333333" />
        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        <pagerstyle backcolor="#336666" forecolor="White" horizontalalign="Center" />
        <rowstyle backcolor="White" forecolor="#333333" />
        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
        <sortedascendingcellstyle backcolor="#F7F7F7" />
        <sortedascendingheaderstyle backcolor="#487575" />
        <sorteddescendingcellstyle backcolor="#E5E5E5" />
        <sorteddescendingheaderstyle backcolor="#275353" />
    </asp:gridview>





C#代码



C# Code

protected void gvdet_sub(object sender, GridViewCommandEventArgs e)
       {
           if (e.CommandName == "cmdname")
           {
               int index = Convert.ToInt32(e.CommandArgument.ToString());

               string str = gvdet.Rows[index].Cells[0].Text.TrimEnd();
               string stnm = gvdet.Rows[index].Cells[1].Text.TrimEnd();
               string stadd = gvdet.Rows[index].Cells[2].Text.TrimEnd();

               ScriptManager.RegisterStartupScript(this, this.GetType(),
               "Message", "alert('Your Detail  is " + str + " "+stnm +" "+stadd +"');", true);

           }
           if (e.CommandName == "data1")
           {
               int i = Convert.ToInt32(e.CommandArgument.ToString());


           }

       }