foreach (DataGridViewRow dgr in dgvBuySell.Rows)
{
if (dgr.Cells["BuySellStates"].Value.ToString() == "Buy") //买
{
dgr.Cells["BuySellBtnBuySell"].Value = "买入";
dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Red; //行变红色
}
if (dgr.Cells["BuySellStates"].Value.ToString() == "Sell") //卖
{
dgr.Cells["BuySellBtnBuySell"].Value = "卖出";
dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Blue; //行变蓝色
}
}
我这样循环datagridview 然后判断现在有个这样的问题,如果我在datagridview的属性里设置AllowUserToAddRows = False 上面的代码就失效了,如果为True才有用,请问为什么会这样?如果要设置False如何才能让上面的代码判断有效?
解决方案 »
- vs2005、vs2010、Oracle在一台pc上的问题
- 怎么用钩子防止外部程序通过句柄方式对我程序进行操作
- C# winform上,label上怎么显示上下的三角形?
- 数据库连接的问题。。。
- winform:判断多个comboBox框输入的数据是不是有重复
- 菜鸟搞个"商品分类显示"搞4天了..救命啊..
- QQ的服务器端是如何得到客户端是否在线的呢?
- 初级问题,答复了立刻给分
- 请问,在c#的windows应用程序弹出的messagebox中,怎么样才能使messagebox显示的信息换行
- 请问哪里又microsoft ado.net 英文电子版下载??
- C#如何实现鼠标移动态显示鼠标所在处的信息
- 熟悉VS中的GRIDVIEW或者DEVEXPRESS中的ASPXGRIDVIEW的朋友请帮帮忙
DataGridView.AllowUserToAddRows 属性
获取或设置一个值,该值指示是否向用户显示添加行的选项。
不过,添加行 一般不用DataGridView.AllowUserToAddRows 属性 而是通过代码来实现
{
if(dgr.Cells["BuySellStates"].Value!=null){
if (dgr.Cells["BuySellStates"].Value.ToString() == "Buy") //买
{
dgr.Cells["BuySellBtnBuySell"].Value = "买入";
dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Red; //行变红色
}
if (dgr.Cells["BuySellStates"].Value.ToString() == "Sell") //卖
{
dgr.Cells["BuySellBtnBuySell"].Value = "卖出";
dgr.Cells["BuySellBtnBuySell"].Style.BackColor = System.Drawing.Color.Blue; //行变蓝色
}
}
}
{
if (e.ColumnIndex == 0)
{
if (e.Value != null)
{
if (e.Value.ToString() == "XB")
{
e.CellStyle.BackColor = Color.Red;
}
else
{
e.CellStyle.BackColor = Color.Blue;
}
}
}
}
想一下每CellFormatting事件就要去遍列所有行,整个显示下来就要重复N多次,当然会狂闪了!按下面代码的思路去调试一下吧,祝你成功!private void dgvBuySell_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgvBuySell[e.RowIndex]["BuySellStates"].ToString()== "Buy")
{
dgvBuySell[e.RowIndex]["BuySellBtnBuySell"].Value = "买入";
e.CellStyle.BackColor = System.Drawing.Color.Red; //当前行变红色
}
if (dgvBuySell[e.RowIndex]["BuySellStates"].ToString()=="Sell")
{
dgvBuySell[e.RowIndex]["BuySellBtnBuySell"].Value = "卖出";
e.CellStyle.BackColor = System.Drawing.Color.Blue; //当前行行变蓝色
}
}
dgvBuySell["BuySellStates",e.RowIndex].Value
DataGridView 取值 dgvBuySell["BuySellStates",RowIndex].Value