请教:grid列动态生成,要可以编辑各单元格,cell的背景也能自己定义颜色

jsoner 2010-05-06 10:04:35
请教:如题,有没有相关的,或可否实现?
...全文
118 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsoner 2010-05-08
  • 打赏
  • 举报
回复
又没人顶了。
jsoner 2010-05-08
  • 打赏
  • 举报
回复
多谢各位。
经和客户沟通,说服他们放弃这种方案了。呵。
结贴,给分。
捷哥1999 2010-05-08
  • 打赏
  • 举报
回复
8楼的思路正确,可以参考。
koukoujiayi 2010-05-08
  • 打赏
  • 举报
回复
动态生成要可以编辑各单元格这相当麻烦!
但设置每个单元格不同颜色还是可以的,在RowCreated事件中进行,以下是设置三行三列不同的颜色:
   protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCellCollection cells = e.Row.Cells;
if (e.Row.RowIndex == 0)//第1行
{
for (int i = 0; i < cells.Count; i++)
{
if (i == 0)//第1单元格
cells[i].BackColor = System.Drawing.Color.Red;
else if (i == 1)//第2单元格
cells[i].BackColor = System.Drawing.Color.Green;
else//其它单元格
cells[i].BackColor = System.Drawing.Color.Blue;
}
}
else if (e.Row.RowIndex == 1)//第2行
{
for (int i = 0; i < cells.Count; i++)
{
if (i == 0)
cells[i].BackColor = System.Drawing.Color.Green;
else if (i == 1)
cells[i].BackColor = System.Drawing.Color.Blue;
else
cells[i].BackColor = System.Drawing.Color.Red;
}
}
else//其他行
{
for (int i = 0; i < cells.Count; i++)
{
if (i == 0)
cells[i].BackColor = System.Drawing.Color.Blue;
else if (i == 1)
cells[i].BackColor = System.Drawing.Color.Red;
else
cells[i].BackColor = System.Drawing.Color.Green;
}
}
}
jsoner 2010-05-06
  • 打赏
  • 举报
回复
忘说明了,webform的
变色不是ROW变色,而是单元格可以变色(背景色可以自己设置,也就是说一行可能有多个颜色)
皇城龙三 2010-05-06
  • 打赏
  • 举报
回复
创建完全可编辑的 DataGrid

http://www.cnblogs.com/qworld/archive/2005/02/28/110282.html
wuyq11 2010-05-06
  • 打赏
  • 举报
回复
wuyq11 2010-05-06
  • 打赏
  • 举报
回复
重写gridview控件
列动态生成可使用ITemplate
RowCreated 中设置
e.Row.BackColor = System.Drawing.Color.Red;
皇城龙三 2010-05-06
  • 打赏
  • 举报
回复
先解决颜色的问题



protected override void Paint(Graphics g, Rectangle Bounds, CurrencyManager Source,
int RowNum, Brush BackBrush, Brush ForeBrush,
bool AlignToRight)
{

bool bdel = (bool) GetColumnValueAtRow(Source, RowNum);

if(bdel == true)
BackBrush = Brushes.Coral;
else
BackBrush = Brushes.White;

g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);

System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif,
(float)8.25 );
g.DrawString( bdel.ToString(), font, Brushes.Black, Bounds.X, Bounds.Y);
}




private void CreateDataGridStyle() 
{
DataGridColumnStyle GridDelColumn;
DataGridColumnStyle GridSeqStyle;
DGStyle = new DataGridTableStyle(); //DGStyle is DataGridTableStyle

DGStyle.MappingName = "Table1";

GridSeqStyle = new DataGridTextBoxColumn();
GridSeqStyle.MappingName = "Column1";
GridSeqStyle.HeaderText = "Column1";
GridSeqStyle.Width = 100;
DGStyle.GridColumnStyles.Add(GridSeqStyle);

PropertyDescriptorCollection pcol = this.BindingContext[myDataSet,
"Table1"].GetItemProperties();

GridDelColumn = new ColumnStyle(pcol["Table1"]);
GridDelColumn.MappingName = "Column2";
GridDelColumn.HeaderText = "Column2";
GridDelColumn.Width = 100;
DGStyle.GridColumnStyles.Add(GridDelColumn);

DGStyle.AllowSorting = true;
DGStyle.RowHeadersVisible = true;
}


完成之后添加到DataGrid中

CreateDataGridStyle();
myDataGrid.TableStyles.Add(DGStyle);

myDataGrid.SetDataBinding(myDataSet,"Table1");


文章原地址:http://www.codeproject.com/KB/cs/custom_datagridcolumnstyl.aspx

可以看看
koukoujiayi 2010-05-06
  • 打赏
  • 举报
回复
这有难度!!帮顶!!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧