I have a gridview in which i populate data about books in a library.
There is a column as 'Book Name' in the gridview.
I want to make this column as linkbutton.
And whenever I will click the linkbuttons, the details of the corresponding book will be displayed in another page.
How can I do this.
Pls help.

You can define TemplateField -> ItemTemplate -> define <asp:HyperLink> in your GridView.

<asp:TemplateField HeaderText="Click">                
  <ItemTemplate>
       <asp:HyperLink ID="link1" runat="server" ></asp:HyperLink>
  </ItemTemplate>
</asp:TemplateField>

Now you just need to handle RawDataBound Event of GridView. In the event, you just need to find the hyperlink column by it's ID like below :

DataRowView rowView = (DataRowView)e.Row.DataItem;
Hyperlink hLink = e.Row.FindControl("id of hyperlink");
hLink.Text = rowView["book_name"].ToString();
hLink.NavigateURL = "test.aspx?bookid=" + rowView["book_id"].ToString();

Now on the test.aspx page, just retrieve query string and pass it in select query to pull all the data related to particular book by id.

hope it will help you :-)

I have a gridview in which i populate data about books in a library.
There is a column as 'Book Name' in the gridview.
I want to make this column as linkbutton.
And whenever I will click the linkbuttons, the details of the corresponding book will be displayed in another page.
How can I do this.
Pls help.

Ok, but the problem is I am populating data to a dynamic gridview, so I need to set generateautocolumns=true.

The aspx code is

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 
            
            EmptyDataText="There are no data records to display." 
            onrowdatabound="GridView1_RowDataBound" >
            <Columns>
            
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="bid" SortExpression="bid">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("book_id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("book_id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="bname" SortExpression="bname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("book_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("book_name") %>' 
                            Visible="False"></asp:Label>
                        <asp:LinkButton ID="l1" runat="server" PostBackUrl="~/codes.aspx"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

And in my .cs file, 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal))
        {
            LinkButton l1 = (LinkButton)e.Row.FindControl("l1");
            Label bname = (Label)e.Row.FindControl("Label1");
            Label bid = (Label)e.Row.FindControl("Label2");

            l1.Text = bname.Text;
            l1.PostBackUrl = "~/codes.aspx?bid=" + bid.Text;


        }
    }

Now, the problem is the gridview shows book_id, book_name(Hyperlinked) and then again the whole data(book_id, book_name,book_author,book_price etc) as coming from database as the autogeneratecolumn is set true. I cant set it false as I need to make the gridview dynamic.
And also, I cannot make the autogenerated book_id and book_name column invisible by writing Gridview1.columns[3].visible=false; Because, column 3 is not been recognised as I made just two columns in <columns> tag.
What to do?
I need to set those second book_id and book_name column invisible. Pls help.

Set AutoGenerateColumns = False. Then in your Select Query just pull BookID and BookName column as you are displaying only bookid and bookname column...

Ok, but the problem is I am populating data to a dynamic gridview, so I need to set generateautocolumns=true.

The aspx code is

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 

            EmptyDataText="There are no data records to display." 
            onrowdatabound="GridView1_RowDataBound" >
            <Columns>

                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="bid" SortExpression="bid">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("book_id") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("book_id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="bname" SortExpression="bname">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("book_name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("book_name") %>' 
                            Visible="False"></asp:Label>
                        <asp:LinkButton ID="l1" runat="server" PostBackUrl="~/codes.aspx"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

And in my .cs file,

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal))
        {
            LinkButton l1 = (LinkButton)e.Row.FindControl("l1");
            Label bname = (Label)e.Row.FindControl("Label1");
            Label bid = (Label)e.Row.FindControl("Label2");

            l1.Text = bname.Text;
            l1.PostBackUrl = "~/codes.aspx?bid=" + bid.Text;


        }
    }

Now, the problem is the gridview shows book_id, book_name(Hyperlinked) and then again the whole data(book_id, book_name,book_author,book_price etc) as coming from database as the autogeneratecolumn is set true. I cant set it false as I need to make the gridview dynamic.
And also, I cannot make the autogenerated book_id and book_name column invisible by writing Gridview1.columns[3].visible=false; Because, column 3 is not been recognised as I made just two columns in <columns> tag.
What to do?
I need to set those second book_id and book_name column invisible. Pls help.

i cant Set AutoGenerateColumns = False as my gridview is not to be made static.
The same gridview is used to fetch other data coming from other queries as well , so I cant Set AutoGenerateColumns = False
I mentioned also before about it
What to do to solve it without Setting AutoGenerateColumns = False
pls help

Member Avatar for siju kuriakose

Hi ,

Pls add a hyperlink to your gridview and bind the ID like this..

<asp:HyperLink runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"BookName")%>' NavigateUrl='<%# "~/BookView.aspx?id=" + DataBinder.Eval(Container.DataItem,"BookID")%>' ID="Hyperlink1" />.

hope your reply

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.