将gridview中的现有列作为超链接 [英] Make an existing column in a gridview as a hyperlink
本文介绍了将gridview中的现有列作为超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical">
<RowStyle BackColor="#F7F7DE" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<EmptyDataTemplate>
No Data Found
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
代码背后的代码
on code behind I
protected void Page_Load(object sender, EventArgs e)
{DataSet dt = GetExistingRecords();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataSet GetExistingRecords()
{
DataSet dt = new DataSet();
try
{
OleDbConnection thisConnection = new OleDbConnection(connStr);
thisConnection.Open();
string query = "Select Call, EnteredYear, Qrt_Mnth, CreateDt, FilingFile, FilingVolSr, ProjNbr from tbl_DataTracking";
OleDbCommand command = new OleDbCommand(query, thisConnection);
var myAdapptor = new OleDbDataAdapter();
//OleDbCommand command = new OleDbCommand("SELECT * FROM tbl_EStamps", thisConnection);
myAdapptor.SelectCommand = command;
myAdapptor.Fill(dt);
thisConnection.Close();
}
catch(Exception e)
{
Response.Write("Error -> " + e);
}
return dt;
}<pre />
now here in my grid view i do not have bound fields.
if I add bound fields the grid containsduplicates
i want the first column to be hyperlink.
Please tell me how to do it.
推荐答案
试试这个:
Try this:
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text });
}
}
请注意,如果您只在第一次加载页面时将数据绑定到网格,那么您的更改将会消失。
找到它:这里 [ ^ ]
Be warned that if you bind data to grid only first time page loaded then your changes will disappear.
Found it: here[^]
ou have to make that column as Template Column
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text="test" NavigateUrl='<%# Eval("fieldName", "show.aspx?ID={0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
这很有用。我确实遇到了要更改列的ID的问题。所以我写了一个程序来按名称获取列ID。
在我的情况下,我一直在寻找Path作为我的专栏名称。
This worked great. I did have an issue getting the ID of the column to change. So I wrote a procedure to get column id by Name.
In my case I was looking for "Path" as my column name.
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[GetColumnIndexByName(e.Row,"Path")];
firstCell.Controls.Clear();
//firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text, Target = "_blank" });
firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = "Click Here", Target = "_blank" });
}
}
private int GetColumnIndexByName(GridViewRow row, string columnName)
{
int columnIndex = 0;
int foundIndex = -1;
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.ContainingField is BoundField)
{
if (((BoundField)cell.ContainingField).DataField.Equals(columnName))
{
foundIndex = columnIndex;
break;
}
}
columnIndex++; // keep adding 1 while we don't have the correct name
}
return foundIndex;
}
这篇关于将gridview中的现有列作为超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文