Ovic
Ovic

Reputation: 45

Want to show data for a specific row in the gridview when that row's buttonfield is clicked

I saw some similar post to this topic but could not solve my problem. I am using access database. So this page will load the data from the database, but it will not show any data until the button for that specific row is clicked. For now it is showing all data. Here is my code please suggest me something.

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

Upvotes: 0

Views: 1704

Answers (1)

Rahul Singh
Rahul Singh

Reputation: 21795

If you are fine to replace your Customer Id BoundField with a template field just like how your customer Name looks then it will be pretty simple:-

<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>

You can simple set the Visibility of your controls to false as I did and in the RowCommand event display them on button click:-

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

With BoundControls you will have to explicitly hide the data and header and it will complicate things.

Upvotes: 1

Related Questions