想要在gridview中显示特定行的数据,当行's按钮字段被单击时

本文关键字:当行 按钮 单击 字段 数据 gridview 显示 | 更新日期: 2023-09-27 18:16:38

我看到了一些类似的帖子,但无法解决我的问题。我正在使用access数据库。因此,这个页面将从数据库加载数据,但在单击特定行的按钮之前,它不会显示任何数据。现在它显示了所有的数据。这是我的代码,请给我一些建议。

    <columns>
      <asp:buttonfield buttontype="Button" 
        commandname="Select"
        headertext="Select Customer" 
        text="Select"/>
        <asp:boundfield datafield="Customer ID" readonly="true" headertext="Customer ID"/>
            <asp:TemplateField HeaderText="First Name" AccessibleHeaderText="FirstName">
             <ItemTemplate>
              <asp:Label ID ="txtEmployeeName" runat ="server" Text='<%# Eval("firstName") %>'></asp:Label>
             </ItemTemplate>
            </asp:TemplateField>
    </columns>

public partial class DisplayCustomer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    DataLayer.DataConnector dat = new DataLayer.DataConnector(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'Ovich'Documents'Visual Studio 2013'WebSites'WebSite3'App_Data'Customer.accdb");
    DataTable dt = dat.DataSelect("SELECT* from Customer");
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
}
public void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple ButtonField column fields are used, 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);
        // Get the last name of the selected author from the appropriate
        // cell in the GridView control.
        GridViewRow selectedRow = GridView1.Rows[index];
    }
}
}

想要在gridview中显示特定行的数据,当行's按钮字段被单击时

如果您可以将Customer Id BoundField替换为模板字段,就像您的客户名称一样,那么它将非常简单:-

<columns>
   <asp:Buttonfield buttontype="Button" commandname="Select" headertext="Select Customer" 
        text="Select"/>
   <asp:TemplateField HeaderText="Employee Id" AccessibleHeaderText="FirstName">
        <ItemTemplate>
           <asp:Label ID ="lblEmployeeId" runat ="server" 
                    Text='<%# Eval("EmployeeId") %>' Visible="false"></asp:Label>
        </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText="First Name" AccessibleHeaderText="FirstName">
        <ItemTemplate>
            <asp:Label ID ="txtEmployeeName" runat ="server" 
                       Text='<%# Eval("firstName") %>' Visible="false"></asp:Label>
        </ItemTemplate>
   </asp:TemplateField>
</columns>

你可以像我一样简单地将控件的可见性设置为false,并在RowCommand事件中显示它们,单击按钮:-

protected void CustomersGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        int index = Convert.ToInt32(e.CommandArgument);
       ((Label)grdCustomer.Rows[index].FindControl("lblEmployeeId")).Visible = true;  
       ((Label)grdCustomer.Rows[index].FindControl("txtEmployeeName")).Visible = true;
    }
}

使用BoundControls,您将不得不显式隐藏数据和头,这将使事情复杂化。