[在线等]DataGridView 的几个问题 就急!!!!!!!!!!!

天-笑 2010-09-16 01:17:31
我的DataGridView 的数据源是 绑定的数据库查询结果集
我的结果集:

编号 状态1 状态2 状态3 状态4
2070204 异常 正常 正常 正常
2080201 正常 正常 正常 正常

现在有几个问题:
1:如何将第一列设为冻结。
2:判断单元格里的值为异常后 将该单元格背景颜色置为红色
3:绑定后如何调整 各列的列宽???
...全文
157 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
a312983516 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 changjiangzhibin 的回复:]
C# code

/*
1:如何将第一列设为冻结。
2:判断单元格里的值为异常后 将该单元格背景颜色置为红色
3:绑定后如何调整 各列的列宽???
*/

public Form2()
{
InitializeComponent();
Bind();
dataGridView1.RowP……
[/Quote]

正解
yinguijian 2010-09-16
  • 打赏
  • 举报
回复
学习学习
oioixiabings 2010-09-16
  • 打赏
  • 举报
回复
学习了!
oioixiabings 2010-09-16
  • 打赏
  • 举报
回复
学习中
ttjxhp 2010-09-16
  • 打赏
  • 举报
回复
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
changjiangzhibin 2010-09-16
  • 打赏
  • 举报
回复

/*
1:如何将第一列设为冻结。
2:判断单元格里的值为异常后 将该单元格背景颜色置为红色
3:绑定后如何调整 各列的列宽???
*/

public Form2()
{
InitializeComponent();
Bind();
dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
}

void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
for (int r = 0; r < dataGridView1.Rows.Count - 1; r++)
{
for (int c = 0; c < dataGridView1.Columns.Count; c++)
{
string str = dataGridView1[c, r].Value.ToString();
if (str == "异常") dataGridView1.Rows[r].Cells[c].Style.BackColor = Color.Red;
}
}
}

private void Bind()
{
DataTable dt = new DataTable();
dt.Columns.Add("编号", typeof(string));
dt.Columns.Add("状态1", typeof(string));
dt.Columns.Add("状态2", typeof(string));
dt.Columns.Add("状态3", typeof(string));
dt.Columns.Add("状态4", typeof(string));
AddValueToDataRow(dt, "2070204", "异常", "正常", "正常", "正常");
AddValueToDataRow(dt, "2080201", "正常", "正常", "正常", "正常");
dataGridView1.DataSource = dt.DefaultView;
dataGridView1.Columns[0].Frozen = true;

}

private void AddValueToDataRow(DataTable dt, string c1, string c2, string c3, string c4, string c5)
{
DataRow addDR = dt.NewRow();
addDR[0] = c1;
addDR[1] = c2;
addDR[2] = c3;
addDR[3] = c4;
addDR[4] = c5;
dt.Rows.Add(addDR);
}
qiuxin425 2010-09-16
  • 打赏
  • 举报
回复
第一列设为冻结:点击编辑列--选择第一列--将属性Forzen设置为true
天-笑 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ycagri 的回复:]
关于2,可以在DataGridView.DataBindingComplete 事件里面处理
[/Quote]
帮忙写个例子吧
天-笑 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qiuxin425 的回复:]
列冻结,使用DataGridViewColumn.Frozen 属性为 True

红色 DataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red;

列宽 Column1.Width = 你想要的值(这是第一列)
[/Quote]
帮忙写个例子吧
ycagri 2010-09-16
  • 打赏
  • 举报
回复
关于2,可以在DataGridView.DataBindingComplete 事件里面处理
qiuxin425 2010-09-16
  • 打赏
  • 举报
回复
列冻结,使用DataGridViewColumn.Frozen 属性为 True

红色 DataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red;

列宽 Column1.Width = 你想要的值(这是第一列)
ycagri 2010-09-16
  • 打赏
  • 举报
回复
列冻结,使用DataGridViewColumn.Frozen 属性为 True
天-笑 2010-09-16
  • 打赏
  • 举报
回复
没人会吗?

111,129

社区成员

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

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

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