Pedram
Pedram

Reputation: 2611

Get data field of the selected row in Gridview using button field in ASP.net

There are two pages named: People.aspx and Profile.aspx.

People.aspx -> contains the name of all users

Profile.aspx -> contains the information of each user

In the first page, People.aspx, I have a Gridview named peopleGridview and when I am loading the page name People.aspx, in Page_Load event I call a method that executes a sql query on database and gets all the fields that I need to put in my Gridview.

The SQL query is like this:

public DataSet PeopleList()
    {
        GetConnection();
        SqlCommand command = new SqlCommand("SELECT Members.ID, Members.Full_Name, Members.Position, Members.Status, Profile.Address" +
                                            " FROM Members" +
                                            " INNER JOIN Profile" +
                                            " ON Members.ID = Profile.ID"+
                                            " ORDER BY Members.Priority, Members.Membership_Date, Members.Full_Name", con);
        SqlDataAdapter da = new SqlDataAdapter(command);//da: DataAdapter 
        DataSet ds = new DataSet();//ds: Dataset
        da.Fill(ds);
        con.Close();
        return ds;
    }

In Gridview I only need to show the fields Full_Name, Position, Status and Address and I do not want to show the value of the field ID (because it is not necessary for ordinary users to know the ID of other users).

I have a button field in my Gridview for each row and I want whenever that I clicked on the button field of the selected row, read the field ID of the selected row (that is not currently shown in Gridview) and put it in the Response.Redirect("Profile.aspx?UserID=" + userid.Text.ToString()); as an argument for the userid in order to send it to Profile.aspx that is another page that contains the information about that user.

How do I do such a work? In addition, how can I get the value of UserID on the side of Profile.aspx

I did the work using Link field in Gridview, however I am not able to do such a work using button field in GridView.

Upvotes: 0

Views: 5748

Answers (1)

Krunal Patil
Krunal Patil

Reputation: 3676

To get specific columns in grid view first set false value to Auto Generate Column AutoGenerateColumns="False" than bind your required field inside the Gridview Column.

html GridView :

<asp:GridView ID="GridView1" CssClass="gridview" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" >
    <Columns>
        <asp:BoundField DataField="Full_Name"  HeaderText="Full Name" SortExpression="Full Name" />
        <asp:BoundField DataField="Position" HeaderText="Postion" SortExpression="Position" />
        <asp:BoundField DataField="Address"  HeaderText="Address" SortExpression="Address" />
        <asp:TemplateField HeaderText="View Profile">
            <ItemTemplate>
                <div style="text-align: center;">
                    <asp:ImageButton ID="ImgBtnViewInq" runat="server" ImageUrl="~/images/Viewmail.png" 
                        CommandName="ViewProfile" CommandArgument='<%# Eval("UserID") %>' ToolTip="View Profile" />
                </div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C# Code behind :

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ViewProfile")
    {
        // id with get the Userid parameter that we set inside the button CommandArgument='<%# Eval("UserID") %>'
        int id = Convert.ToInt32(e.CommandArgument);

        // Send this value of id to your querystring to redirect to another page
        Response.Redirect("Profile.aspx?UserId=" + id);
    }
}

Upvotes: 3

Related Questions