Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I have my datagridview filled which have 7 columns.Now I want to add link to 1 column(i.e Number).After clicking this link,it will display details based on that number.
Please can any1 help me out?
Posted
Updated 12-Nov-19 21:11pm

VB
Dim lnk As New DataGridViewLinkColumn()
       DataGridView1.Columns.Add(lnk)
       lnk.HeaderText = "Link Data"
       lnk.Name = "Link"
       lnk.Text = "Link"
       lnk.UseColumnTextForLinkValue = True



And Use the DataKeyNames(PrimaryKey index) and the value of the column to get the data to display the results.


http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewlinkcolumn.aspx[^]
 
Share this answer
 
v2
XML
<asp:GridView ID="GridView1" runat="server" EnableViewState="false" AutoGenerateColumns="False"
    ShowFooter="True" DataKeyNames="VendorId" AllowPaging="True" CellPadding="3"
    PageSize="5" BackColor="White" BorderColor="White" BorderStyle="Ridge" CellSpacing="1"
    Width="100%" BorderWidth="2px" GridLines="None" AllowSorting="true"  OnRowDataBound="gv_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Edit" ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="complain_no" runat="server" CausesValidation="False" CommandName="complain_no"
                    Text="complain_no"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="date" SortExpression="VendorFName">

            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("date") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="name" SortExpression="VendorLName">
            <ItemTemplate>
                <asp:Label ID="name" runat="server" Text='<%# Bind("name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        
    </Columns>
    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
    <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#C6C3C6" />
    <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>



protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       // Use debug and see the column which u got. 
       // check the column. if its linkbutton column then
       // do your code here.

    }

}
 
Share this answer
 
v2
Comments
Rachna0309 20-Oct-12 2:34am    
I am working on windows application so this code wont work..
Sanjeev Alamuri 20-Oct-12 2:47am    
okay, but try to mention all details.
In ur design mode right click on gridview and click on edit columns then add column. there you can find DataGridViewLinkColumn. chose and apply then ok.
and add CellClick event to the grid from Properties pane. There after the code behind code as follows.

C#
private void DgvView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 0)//if ur link columnIndex (complain_no ColumnIndex) is zero 
                {
                  // Display your data here
                }
            } 
            catch(Exception e)
            {
            }
         }



// same procedure in vb.net also. so convert into vb.net code.
 
Share this answer
 
v2
Comments
Rachna0309 20-Oct-12 2:52am    
Thanx for the help...
I have already tried this.But I don't want to add new column.I have complain_no,date,name etc as columns.My task is I want to add link to complain_no column which after clicking will display details related to that number.
 
Share this answer
 
Comments
Member 13375990 1-Sep-17 2:38am    
@rachna iam also looking for the same query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900