DataGridView 选择单元格改变行背景色
publicForm1(){InitializeComponent();setdgv();}publicvoidsetdgv(){DataTabledt=newDataT...
public Form1()
{
InitializeComponent();
setdgv();
}
public void setdgv()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("sex", typeof(string));
dt.Rows.Add("aa","man");
dt.Rows.Add("bb", "man");
dt.Rows.Add("cc", "man");
dt.Rows.Add("dd", "man");
dt.Rows.Add("ee", "man");
dt.Rows.Add("ff", "man");
dt.Rows.Add("gg", "man");
dt.Rows.Add("hh", "man");
dataGridView1.DataSource = dt;
}
//改变单元格背景
DataGridViewSelectedCellCollection dsc;
private void EcDataGridView_SelectionChanged(object sender, EventArgs e)
{
try
{
DataGridView dgv_c = (DataGridView)sender;
//上一次的选着
if (dsc != null)
{
for (int i = 0; i < dsc.Count; i++)
{
if (dsc[i].RowIndex >= 0)
dgv_c.Rows[dsc[i].RowIndex].DefaultCellStyle.BackColor = new Color();
}
}
//这次的选着
dsc = dgv_c.SelectedCells;
for (int i = 0; i < dgv_c.SelectedCells.Count; i++)
{
dgv_c.Rows[dgv_c.SelectedCells[i].RowIndex].DefaultCellStyle.BackColor = Color.SkyBlue;
}
}
catch (Exception) { return; }
}
自己写的代码。现在执行后全部行都变色了。请高人指点一下 展开
{
InitializeComponent();
setdgv();
}
public void setdgv()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("sex", typeof(string));
dt.Rows.Add("aa","man");
dt.Rows.Add("bb", "man");
dt.Rows.Add("cc", "man");
dt.Rows.Add("dd", "man");
dt.Rows.Add("ee", "man");
dt.Rows.Add("ff", "man");
dt.Rows.Add("gg", "man");
dt.Rows.Add("hh", "man");
dataGridView1.DataSource = dt;
}
//改变单元格背景
DataGridViewSelectedCellCollection dsc;
private void EcDataGridView_SelectionChanged(object sender, EventArgs e)
{
try
{
DataGridView dgv_c = (DataGridView)sender;
//上一次的选着
if (dsc != null)
{
for (int i = 0; i < dsc.Count; i++)
{
if (dsc[i].RowIndex >= 0)
dgv_c.Rows[dsc[i].RowIndex].DefaultCellStyle.BackColor = new Color();
}
}
//这次的选着
dsc = dgv_c.SelectedCells;
for (int i = 0; i < dgv_c.SelectedCells.Count; i++)
{
dgv_c.Rows[dgv_c.SelectedCells[i].RowIndex].DefaultCellStyle.BackColor = Color.SkyBlue;
}
}
catch (Exception) { return; }
}
自己写的代码。现在执行后全部行都变色了。请高人指点一下 展开
3个回答
展开全部
看看代码就知道了,要在Form—load里面对dsc进行赋初值
注意添加Form1_Load事件
public Form1()
{
InitializeComponent();
setdgv();
}
public void setdgv()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("sex", typeof(string));
dt.Rows.Add("aa", "man");
dt.Rows.Add("bb", "man");
dt.Rows.Add("cc", "man");
dt.Rows.Add("dd", "man");
dt.Rows.Add("ee", "man");
dt.Rows.Add("ff", "man");
dt.Rows.Add("gg", "man");
dt.Rows.Add("hh", "man");
dataGridView1.DataSource = dt;
}
//改变单元格背景
DataGridViewSelectedCellCollection dsc;
private void EcDataGridView_SelectionChanged(object sender, EventArgs e)
{
try
{
DataGridView dgv_c = (DataGridView)sender;
//上一次的选着
if (dsc != null)
{
for (int i = 0; i < dsc.Count; i++)
{
if (dsc[i].RowIndex >= 0)
dgv_c.Rows[dsc[i].RowIndex].DefaultCellStyle.BackColor = new Color();
}
}
else
{
return;
}
//这次的选着
dsc = dgv_c.SelectedCells;
for (int i = 0; i < dgv_c.SelectedCells.Count; i++)
{
dgv_c.Rows[dgv_c.SelectedCells[i].RowIndex].DefaultCellStyle.BackColor = Color.SkyBlue;
}
}
catch (Exception) { return; }
}
private void Form1_Load(object sender, EventArgs e)
{
dsc = dataGridView1.SelectedCells;
}
注意添加Form1_Load事件
public Form1()
{
InitializeComponent();
setdgv();
}
public void setdgv()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("sex", typeof(string));
dt.Rows.Add("aa", "man");
dt.Rows.Add("bb", "man");
dt.Rows.Add("cc", "man");
dt.Rows.Add("dd", "man");
dt.Rows.Add("ee", "man");
dt.Rows.Add("ff", "man");
dt.Rows.Add("gg", "man");
dt.Rows.Add("hh", "man");
dataGridView1.DataSource = dt;
}
//改变单元格背景
DataGridViewSelectedCellCollection dsc;
private void EcDataGridView_SelectionChanged(object sender, EventArgs e)
{
try
{
DataGridView dgv_c = (DataGridView)sender;
//上一次的选着
if (dsc != null)
{
for (int i = 0; i < dsc.Count; i++)
{
if (dsc[i].RowIndex >= 0)
dgv_c.Rows[dsc[i].RowIndex].DefaultCellStyle.BackColor = new Color();
}
}
else
{
return;
}
//这次的选着
dsc = dgv_c.SelectedCells;
for (int i = 0; i < dgv_c.SelectedCells.Count; i++)
{
dgv_c.Rows[dgv_c.SelectedCells[i].RowIndex].DefaultCellStyle.BackColor = Color.SkyBlue;
}
}
catch (Exception) { return; }
}
private void Form1_Load(object sender, EventArgs e)
{
dsc = dataGridView1.SelectedCells;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设置CellClick就可以了,其实那条语句一起生效,之所以看不出来效果是因为,那一行处于选中状态,显示的是默认选择的颜色,当再点另外一行的时,就可以看出背景色已经改变了。
在代码之前添加:
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
另外 click 后,被选中的单元格,还有个 SelectionBackColor 设置。
在代码之前添加:
if (e.ColumnIndex < 0 || e.RowIndex < 0)
return;
另外 click 后,被选中的单元格,还有个 SelectionBackColor 设置。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
cell不是列吗,你不是要变row吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |