如何在网格视图中使用行索引和单元格值来查找列索引
本文关键字:索引 单元格 查找 网格 视图 | 更新日期: 2023-09-27 18:24:06
我正在创建一个动态网格视图,该视图具有动态链接按钮,单击该按钮可以捕获行索引和列索引。我可以捕获行索引和单元格值,但不能捕获列索引。。。我想我可以通过单元格索引做到这一点,但如何找到单元格索引。。。请帮忙,我从几个小时开始搜索。。。
protected void GDVReports_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
LinkButton myLink = new LinkButton();
myLink.Click += ViewDetails;
if (cell.Controls.Count > 0)
{
while (cell.Controls.Count > 0)
{
myLink.Controls.Add(cell.Controls[0]);
}
}
else
{
myLink.Text = cell.Text;
}
cell.Controls.Add(myLink);
}
}
}
protected void ViewDetails(object sender, EventArgs e)
{
LinkButton lnkView = (sender as LinkButton);
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
string id = lnkView.CommandArgument;
string b = row.DataItemIndex.ToString();
int d=Convert.ToInt16(b);
string r = //to find cell index
rowname = GDVReports.Rows[d].Cells[0].Text.ToString();
string columnIndex = GDVReports.Rows[d].Cells[r].Text.ToString();
int c = Convert.ToInt32(columnIndex);
columnname = GDVReports.HeaderRow.Cells[c].Text.ToString();
}
如果适用,单元格有一个名为ColumnIndex的比例。。。如果您只有单元格值,请在给定行内的列中搜索与您的值匹配的列。。。
不幸的是,GridView没有为任何it事件或其他任何地方提供属性:ColumnIndex
。
为什么不创建自己的逻辑,根据单击的LinkButton
的ID
提取Cell Index呢。下面是一个示例代码,可以帮助您入门。
LinkButton lnkView = (sender as LinkButton); // This button was clicked
GridViewRow row = (lnkView.NamingContainer as GridViewRow);
int cellIndex = 0;
foreach( TableCell cell in row.Cells)
{
if(cell.FindControl(lnkView.ID) !=null)
break;
else
cellIndex++;
}
//现在cellIndex
具有单击LinkButton
的Column索引,
//所以获取列名
columnname = GDVReports.HeaderRow.Cells[cellIndex].Text.ToString();