问题描述
我已填充7列的datagridview.现在我想向1列(即数字)添加链接.单击此链接后,它将显示基于该数字的详细信息.
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?
推荐答案
Dim lnk As New DataGridViewLinkColumn()
DataGridView1.Columns.Add(lnk)
lnk.HeaderText = "Link Data"
lnk.Name = "Link"
lnk.Text = "Link"
lnk.UseColumnTextForLinkValue = True
并使用DataKeyNames(PrimaryKey索引)和列的值来获取数据以显示结果.
http://msdn.microsoft.com/en-us/library/system. windows.forms.datagridviewlinkcolumn.aspx [ ^ ]
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[^]
<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.
}
}
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)
{
}
}
//在vb.net中也执行相同的步骤.因此请转换为vb.net代码.
// same procedure in vb.net also. so convert into vb.net code.
这篇关于动态地将链接添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!