C# ASP.NET GridView有没有一种方法可以根据您所在的单元格获取列名 受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e) { 如果(e.Row.RowType==DataControlRowType.DataRow) { foreach(e.Row.Cells中的表格单元格) { //……这里。。。。 cell.Text=cell.Text.Replace(entry.Value,“+entry.Value+”); } } }
上面写着。。。。在这里我需要以某种方式获得DataGrid的列名。因此,根据我当前所在的单元格,我需要获得一个列名。在后面的代码中,根据我所在的列,我需要对单元格进行不同的格式化。此外,列名在运行时是不知道的,它们是动态生成的,因此我需要一个通用方法来获取列名 您可以更改循环的方式,并制作如下内容:C# ASP.NET GridView有没有一种方法可以根据您所在的单元格获取列名 受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e) { 如果(e.Row.RowType==DataControlRowType.DataRow) { foreach(e.Row.Cells中的表格单元格) { //……这里。。。。 cell.Text=cell.Text.Replace(entry.Value,“+entry.Value+”); } } },c#,asp.net,gridview,C#,Asp.net,Gridview,上面写着。。。。在这里我需要以某种方式获得DataGrid的列名。因此,根据我当前所在的单元格,我需要获得一个列名。在后面的代码中,根据我所在的列,我需要对单元格进行不同的格式化。此外,列名在运行时是不知道的,它们是动态生成的,因此我需要一个通用方法来获取列名 您可以更改循环的方式,并制作如下内容: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowT
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
// .... here ....
cell.Text = cell.Text.Replace(entry.Value, "<span class='highlightSearchTerm'>" + entry.Value + "</span>");
}
}
}
for(int i=0;i
您可以直接看到单元格
和行
。gvMyLista
是GridView的id
现在,如果您在这里进行格式化,它可能会在回发后更改,因为数据可能不会再次绑定,因此我建议在PreRender上进行格式化,这里有一个类似的问题/a:尝试以下方法:
for (int i = 0; i < gvMyLista.Rows.Count; i++)
{
// get your data as...
// gvMyLista.Rows[i].Cells[2].Text
}
通过列名,如果您的意思是
标题文本
,那么您可以使用for
循环而不是foreach
,使用索引来获取标题文本
,如下所示:
string currentColName = GridView1.Columns[GridView1.CurrentCell.ColumnIndex].Name;
受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
for(int i=0;i
完美。我唯一要添加的是code
Icode
由于某种原因,它超出了范围。GridView1.Columns[0]。Name
是第一列的名称,GridView1.CurrentCell.ColumnIndex
是一个整数,等于当前单元格(选定单元格)的列数. 我没有测试这段代码,但它应该可以工作,工作正常吗?(编辑:使我的评论更容易阅读)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
TableCell cell = e.Row.Cells[i];
string HeaderText = yourGridView.Columns[i].HeaderText;
cell.Text = cell.Text.Replace(entry.Value,
"<span class='highlightSearchTerm'>" + entry.Value + "</span>");
}
}
}
}