2011-07-07 25 views

回答

2

到GridView

添加OnRowDataBound事件处理程序在事件处理程序 - 检查头部和处理每列相应

protected virtual void OnRowDataBound(GridViewRowEventArgs e) { 
    if (e.Row.RowType == DataControlRowType.Header) { 
     // process your header here.. 
    } 
} 

欲了解更多信息请点击这里:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewroweventargs.row.aspx

+1

将图像添加到headertemplate并在onrowdatabound事件中隐藏它或将其分配给一个url。 – anami

0

我是不完全确定如何添加图像......但this link有一个在行标题中编写数字的好例子,因此您可以使用> 10条件更新它以显示图像。 。

+0

我不想号码添加到行标题。我想在不同的场合添加不同的图像添加行标题。请检查阅读的单元格内容。您可以帮助我...... – hmlasnk

+0

哦对不起,我错过了单元格值检查,我不确定是否可以做到这一点通过这个事件。抱歉! – nbz

2

你可以将图像添加到DataGridView.RowPostPaint事件中的DataGridView行标题中。

下面是在CodeProject上的文章,似乎描述的这个相当不错的链接(我还没有尝试过对自己代码):Row Header Cell Images in DataGridView

可以使用RowPostPaint事件提取要测试对值确定要显示哪个图标。通过使用该事件的RowIndex属性与你感兴趣的列的索引值做到这一点

像这样的东西应该作为一个起点:

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { 
    // As an example we'll check contents of column index 1 in our DGV. 
    string numberString = dataGridView1.Rows[e.RowIndex].Cells[1].Value as string; 
    if (numberString != null) { 
     int number; 
     if (Int32.TryParse(numberString, out number)) { 
      if (number > 10) { 
       // Display one icon. 
      } else { 
       // Display the other icon. 
      } 
     } else { 
      // Do something because the string that is in the cell cannot be converted to an int. 
     } 
    } else { 
     // Do something because the cell Value cannot be converted to a string. 
    } 
} 
+0

Thiks很多。这对我的程序有用..... thnx再次... – hmlasnk

相关问题