【问题标题】:Selecting Particular Column in a Gridview在 Gridview 中选择特定列
【发布时间】:2019-05-28 23:08:25
【问题描述】:

我有一个网格视图,其中显示了多条记录。在页面加载事件中,我正在从数据库中检索数据并填充 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 并将其发送到另一个页面。请帮忙

【问题讨论】:

  • 您希望/已经将哪一列设置为 ID?我认为您可以将SelectedRowDataKeyNames 用于特定列。

标签: c# asp.net .net gridview datagridview


【解决方案1】:

e.CommandArgument 包含您点击它的索引。 dgvEmployeesInformation.SelectedValue 包含您在 DataKeyNames 上设置的 ID。

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);
        }
    }
}

【讨论】:

    【解决方案2】:

    试试这个

    void yourGridView_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=="Edit")
    {
    // 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 = ContactsGridView.Rows[index];
    var yourID = row.Cells[0].Text;
    //....
    }
    }
    

    Grid view docs.microsoft.com

    【讨论】:

    • 我有一个 ImageButton,我想获取隐藏在 gridview 中的 Id,当用户单击 imageButton 时,他/她使用该行的 Id 重定向到另一个网络表单。 ContactsGridView 给我一个错误,Does not exist in the current context
    • 用您的网格视图名称更改它(对不起,我没有使用您的变量名称)
    • 它不起作用,因为当我用 ImageButton 输入投射它时,它会抛出错误,if (e.CommandName == "Edit") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = dgvEmployeesInformation.Rows[Index]; var UserId= row.Cells[0].Text; Response.Redirect("~/AddEmployeeBasic1.aspx?Id=" + ((ImageButton)sender).ToString()); }
    • 为什么放这个:Response.Redirect("~/AddEmployeeBasic1.aspx?Id=" + ((ImageButton)sender).ToString());我认为你应该使用 UserId 而不是 ((ImageButton)sender).ToString())... 类似 Response.Redirect("~/AddEmployeeBasic1.aspx?Id=" + UserId) ;
    • 为此,我必须获取 Id,但我无法获取所选行的 Id。如何选择身份证?得到Id后,我就可以重定向了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多