DataGridView中的单元格格式DataBindingComplete [英] Cell Formatting In DataGridView On DataBindingComplete
问题描述
public partial class TestForm2:Form
{
private DataTable dt;
private int methodCalls = 0;
private bool isFormatted = false;
public TestForm2()
{
InitializeComponent();
buildDataTable();
dataGridView1.DataBindingComplete + = dataGridView1_DataBindingComplete;
}
private void TestForm2_Load(object sender,EventArgs e)
{
bindData();
}
private void button1_Click(object sender,EventArgs e)
{
formatDataGridView();
}
private void bindData()
{
dataGridView1.DataSource = dt;
}
private void dataGridView1_DataBindingComplete(object sender,DataGridViewBindingCompleteEventArgs e)
{
//如果此代码被注释掉,程序将工作正常
//只需点击按钮
//这是添加的,以防止formatDataGridView执行更多的
//比一次。即使我注销并重新注册了事件处理程序,
//方法仍然被称为3 - 4次。这个成功的
//阻止了,但只有*被删除,没有红色背景颜色
//添加到单元格。
if(!isFormatted)
{
formatDataGridView();
}
}
private void buildDataTable()
{
dt = new DataTable();
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Columns.Add(col4);
随机randNum = new Random(); (int i = 0; i< 10; i ++)
DataRow dr;
object [] rowItems = new object [dt.Columns.Count];
for(int j = 0; j< dt.Columns.Count; j ++)
{
int number = randNum.Next(1,20);
if(number%7 == 0)
{
rowItems [j] = number +*;
}
else if(number%5 == 0)
{
rowItems [j] = number +**;
}
else
{
rowItems [j] = number;
}
}
dr = dt.NewRow();
dr.ItemArray = rowItems;
dt.Rows.Add(dr);
}
}
private void formatDataGridView()
{
//我注意到我需要将事件处理程序注销到
/ /格式化时阻止DataBindingComplete触发
dataGridView1.DataBindingComplete - = dataGridView1_DataBindingComplete;
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
string originalCell;
string reformattedCell;
if(row.Cells [col1]。Value!= null)
{
originalCell = row.Cells [col1] Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**, );
row.Cells [col1]。Value = reformattedCell;
row.Cells [col1]。Style.BackColor = Color.Red;
row.Cells [col1]。ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
row.Cells [col1]。Value = reformattedCell;
row.Cells [col1]。Style.BackColor = Color.Red;
row.Cells [col1]。ToolTipText =可除以7;
}
else
{
// do nothing
}
}
if(row.Cells [col2]。Value!= null)
{
originalCell = row.Cells [col2]。Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**, );
row.Cells [col2]。Value = reformattedCell;
row.Cells [col2]。Style.BackColor = Color.Red;
row.Cells [col2]。ToolTipText =可除以5;
}
if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace( *,);
row.Cells [col2]。Value = reformattedCell;
row.Cells [col2]。Style.BackColor = Color.Red;
row.Cells [col2]。ToolTipText =可除以7;
}
else
{
// do nothing
}
}
if(row.Cells [col3]。Value!= null)
{
originalCell = row.Cells [col3]。Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**, );
row.Cells [col3]。Value = reformattedCell;
row.Cells [col3]。Style.BackColor = Color.Red;
row.Cells [col3]。ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
row.Cells [col3]。Value = reformattedCell;
row.Cells [col3]。Style.BackColor = Color.Red;
row.Cells [col3]。ToolTipText =可除以7;
}
else
{
// do nothing
}
}
if(row.Cells [col4]。Value!= null)
{
originalCell = row.Cells [col4]。Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**, );
row.Cells [col4]。Value = reformattedCell;
row.Cells [col4]。Style.BackColor = Color.Red;
row.Cells [col4]。ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
row.Cells [col4]。Value = reformattedCell;
row.Cells [col4]。Style.BackColor = Color.Red;
row.Cells [col4]。ToolTipText =可除以7;
}
else
{
// do nothing
}
}
}
//重新注册事件处理程序
dataGridView1.DataBindingComplete + = dataGridView1_DataBindingComplete;
isFormatted = true;
methodCalls ++;
MessageBox.Show(方法调用:+ methodCalls);
}
}
我不知道如何解决这个问题,必须有办法。我最近不熟悉DataBindingComplete,所以我一定要在这里学习一些东西。感谢大家的帮助,帮助我学习新的东西!
CellFormatting事件处理程序是我最终采取的解决方案问题。
private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
string originalCell;
string reformattedCell;
if(this.dataGridView1.Columns [e.ColumnIndex] .Name ==col1)
{
if(e.Value!= null)
{
DataGridViewCell cell =
this.dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex];
originalCell = e.Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可整除7;
}
else
{
// do nothing
}
}
}
if(this.dataGridView1.Columns [e.ColumnIndex] .Name ==col2)
{
if(e.Value!= null)
{
DataGridViewCell cell =
this.dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex];
originalCell = e.Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可整除7;
}
else
{
// do nothing
}
}
}
if(this.dataGridView1.Columns [e.ColumnIndex] .Name ==col3)
{
if(e.Value!= null)
{
DataGridViewCell cell =
this.dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex];
originalCell = e.Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可整除7;
}
else
{
// do nothing
}
}
}
if(this.dataGridView1.Columns [e.ColumnIndex] .Name ==col4)
{
if(e.Value!= null)
{
DataGridViewCell cell =
this.dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex];
originalCell = e.Value.ToString();
if(originalCell.Count(c => c =='*')== 2)
{
reformattedCell = originalCell.Replace(**,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可除以5;
}
else if(originalCell.Count(c => c =='*')== 1)
{
reformattedCell = originalCell.Replace *,);
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText =可整除7;
}
else
{
// do nothing
}
}
}
}
I am having problems with a requirement on one of my projects at work. What I am doing is binding a datatable to a DataGridView(DGV)'s datasource. I then loop through the DataGridView and check if the cell as either 1 * or 2 ** in its value and format those cells with a tooltip and a red background. If I use a button event to trigger this everything works perfectly fine. But if I want this to occur automatically when the form loads using the DataBindingComplete event it does not work correctly. The problem was DataBindingComplete was firing multiple times. I read this SO question which gave me a couple options to try and none worked. Here is the code:
public partial class TestForm2 : Form
{
private DataTable dt;
private int methodCalls = 0;
private bool isFormatted = false;
public TestForm2()
{
InitializeComponent();
buildDataTable();
dataGridView1.DataBindingComplete += dataGridView1_DataBindingComplete;
}
private void TestForm2_Load(object sender, EventArgs e)
{
bindData();
}
private void button1_Click(object sender, EventArgs e)
{
formatDataGridView();
}
private void bindData()
{
dataGridView1.DataSource = dt;
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//If this code is commented out the program will work just fine
//by just clicking the button
//This was added to prevent formatDataGridView from executing more
//than once. Even though I unreg and rereg the event handler, the
//method was still being called 3 - 4 times. This successfully
//prevented that but only the *'s were removed and no red back color
//added to the cells.
if(!isFormatted)
{
formatDataGridView();
}
}
private void buildDataTable()
{
dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
Random randNum = new Random();
for(int i = 0; i < 10; i++)
{
DataRow dr;
object[] rowItems = new object[dt.Columns.Count];
for(int j = 0; j < dt.Columns.Count; j++)
{
int number = randNum.Next(1, 20);
if(number % 7 == 0)
{
rowItems[j] = number + "*";
}
else if(number % 5 == 0)
{
rowItems[j] = number + "**";
}
else
{
rowItems[j] = number;
}
}
dr = dt.NewRow();
dr.ItemArray = rowItems;
dt.Rows.Add(dr);
}
}
private void formatDataGridView()
{
// I noticed that I needed to unregister the event handler to
// prevent DataBindingComplete from firing during the format
dataGridView1.DataBindingComplete -= dataGridView1_DataBindingComplete;
foreach(DataGridViewRow row in dataGridView1.Rows)
{
string originalCell;
string reformattedCell;
if(row.Cells["col1"].Value != null)
{
originalCell = row.Cells["col1"].Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
row.Cells["col1"].Value = reformattedCell;
row.Cells["col1"].Style.BackColor = Color.Red;
row.Cells["col1"].ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
row.Cells["col1"].Value = reformattedCell;
row.Cells["col1"].Style.BackColor = Color.Red;
row.Cells["col1"].ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
if (row.Cells["col2"].Value != null)
{
originalCell = row.Cells["col2"].Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
row.Cells["col2"].Value = reformattedCell;
row.Cells["col2"].Style.BackColor = Color.Red;
row.Cells["col2"].ToolTipText = "Divisible by 5";
}
if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
row.Cells["col2"].Value = reformattedCell;
row.Cells["col2"].Style.BackColor = Color.Red;
row.Cells["col2"].ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
if (row.Cells["col3"].Value != null)
{
originalCell = row.Cells["col3"].Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
row.Cells["col3"].Value = reformattedCell;
row.Cells["col3"].Style.BackColor = Color.Red;
row.Cells["col3"].ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
row.Cells["col3"].Value = reformattedCell;
row.Cells["col3"].Style.BackColor = Color.Red;
row.Cells["col3"].ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
if (row.Cells["col4"].Value != null)
{
originalCell = row.Cells["col4"].Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
row.Cells["col4"].Value = reformattedCell;
row.Cells["col4"].Style.BackColor = Color.Red;
row.Cells["col4"].ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
row.Cells["col4"].Value = reformattedCell;
row.Cells["col4"].Style.BackColor = Color.Red;
row.Cells["col4"].ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
}
// Reregistering the event handler
dataGridView1.DataBindingComplete += dataGridView1_DataBindingComplete;
isFormatted = true;
methodCalls++;
MessageBox.Show("Method Calls: " + methodCalls);
}
}
I am not sure how to get around this problem but there has to be a way. I am unfamiliar with DataBindingComplete until recently so I am sure to learn something here. Thanks for the help everyone and helping me learn something new!
CellFormatting event handler was the route I ended up taking to resolve my issue.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
string originalCell;
string reformattedCell;
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "col1")
{
if(e.Value != null)
{
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
originalCell = e.Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
}
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "col2")
{
if (e.Value != null)
{
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
originalCell = e.Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
}
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "col3")
{
if (e.Value != null)
{
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
originalCell = e.Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
}
if (this.dataGridView1.Columns[e.ColumnIndex].Name == "col4")
{
if (e.Value != null)
{
DataGridViewCell cell =
this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
originalCell = e.Value.ToString();
if (originalCell.Count(c => c == '*') == 2)
{
reformattedCell = originalCell.Replace("**", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 5";
}
else if (originalCell.Count(c => c == '*') == 1)
{
reformattedCell = originalCell.Replace("*", "");
cell.Value = reformattedCell;
cell.Style.BackColor = Color.Red;
cell.ToolTipText = "Divisible by 7";
}
else
{
//do nothing
}
}
}
}
这篇关于DataGridView中的单元格格式DataBindingComplete的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!