首页 > 解决方案 > 在 Gridview 中选择特定列

问题描述

我有一个网格视图,其中显示了多条记录。在页面加载事件中,我正在从数据库中检索数据并填充 gridview。我还有另一列用于编辑和删除,它们是 Imagebuttons。现在我希望当用户单击编辑按钮时,然后基于 Id(用户单击时的该行),他/她重定向到具有所选 Id 的另一个网络表单,然后该人记录从数据库加载并填充相应的文本框。我没有更新 Gridview 中的记录。这是清晰理解的图像 在此处输入图像描述

这是我的 .aspx 代码

<asp:GridView ID="dgvEmployeesInformation" runat="server" CssClass=" table table-bordered table-hover table-responsive" AutoGenerateColumns="false" OnRowCommand="dgvEmployeesInformation_RowCommand">
                <%--1st Column--%>
                <Columns>
                    <asp:BoundField  HeaderText="ID" DataField="Id" ShowHeader="false" Visible="false" ControlStyle-BackColor="#0066ff"/>
                    <asp:BoundField  HeaderText="Name" DataField="Name"/>
                    <asp:BoundField  HeaderText="Employee No" DataField="EmployeeNo"/>
                    <asp:BoundField  HeaderText="Father Name" DataField="FatherName"/>
                    <asp:BoundField  HeaderText="CNIC" DataField="CNIC"/>
                    <asp:BoundField  HeaderText="Contact No" DataField="ContactNo"/>
                    <asp:BoundField  HeaderText="City" DataField="City"/>
                    <asp:BoundField  HeaderText="Post" DataField="RecommendedPost"/>
                    <asp:BoundField  HeaderText="Status" DataField="Status"/>
                    <asp:BoundField  HeaderText="Degree Status" DataField="DegreeStatus"/>
                    <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                            <asp:ImageButton ImageUrl="~/Images/edit.png" CommandName="Edit" Text='<%# Eval("ID") %>' ToolTip="Edit" Width="20px" Height="20px" runat="server"/>
                            <asp:ImageButton ImageUrl="~/Images/delete.png" CommandName="Delete" Text='<%# Eval("ID") %>' ToolTip="Delete" Width="20px" Height="20px" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

这是 .aspx.cs 代码

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dgvEmployeesInformation.DataSource = GetData();
            dgvEmployeesInformation.DataBind();
        }
    }

    private DataSet GetData()
    {
        DataSet DS = new DataSet();
        using (SqlConnection con = new SqlConnection(Base.GetConnection))
        {

            SqlDataAdapter ad = new SqlDataAdapter("spSelectEmployeeSpecificRecord", con);
            ad.SelectCommand.CommandType = CommandType.StoredProcedure;
            ad.Fill(DS);
        }
        return DS;
    }
protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Edit"))
        {

        }
    }

我不知道如何获取所选行的 ID 并将其发送到另一个页面。请帮忙

标签: c#asp.net.netgridviewdatagridview

解决方案


e.CommandArgument包含您单击它的索引。并且dgvEmployeesInformation.SelectedValue包含您设置的 Id DataKeyNames

DataKeyNames在您拥有的数据库字段上设置id- 在您的情况下DataKeyNames="EmployeeNo",代码可以是

protected void dgvEmployeesInformation_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        int SelectedIndex;
        if (int.TryParse(e.CommandArgument.ToString(), out SelectedIndex))
        {
            // make it selected after the click
            dgvEmployeesInformation.SelectedIndex = SelectedIndex;

            // now the SelectedValue contains the id
            Edit(dgvEmployeesInformation.SelectedValue);
        }
    }
}

推荐阅读