Windows Develop Bookmark and Share   
 index > Windows Forms General > Events on DataGridView Load
 

Events on DataGridView Load

Sorry Folks, wrote in portuguese.

I´m a newbie in C# and I´ll need your help many times.
We are migrating an SQLWindows application to C#.

My first question is about events sent when loadind DataGridView.

What events are sent before and after each new row being created?

dataGridView1.DataSource = dt;

I need change some attributes like background color, rowheadertext contents, etc.

Thanks in advantage!

Edson

  • Edited byEdson Nilo Friday, September 11, 2009 4:45 PM
  • Moved byHarry ZhuMSFTMonday, September 14, 2009 4:44 AMrelating to datagridview (From:Visual C# Language)
  •  
Edson Nilo  Friday, September 11, 2009 1:38 PM
Hi Edson Nilo,

I think give you a sample can better explain my meaning. Please look at the following sample.
public partial class Form1 : Form
{
private DataTable dt = new DataTable();
private bool firstRun = true;

public Form1()
{
InitializeComponent();
dt.Columns.Add("Column");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i);
}
dataGridView1.DataSource = dt;
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
}

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (firstRun)
{
foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
{
if (dgvRow.IsNewRow == false && dgvRow.Cells[0].Value.ToString() == "1")
{
dgvRow.Cells[0].Style.BackColor = Color.Red;
}
}
firstRun = false;
}
}

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.FormattedValue.ToString() == "1")
{
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
}
}
}

In this sample, I will set the cell's backcolor to Red whose value is 1, So I handle the CellValidating event to do the check and set. But only handle that event is not enough. When the form is load, you can see the cell whose value is 1 doesn't have red backcolor. That's the reason why I handle DataBindingComplete event to set the color at the first time.

Do you understand my meaning?

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
  • Marked As Answer byEdson Nilo Wednesday, September 16, 2009 9:24 PM
  •  
Kira Qian  Wednesday, September 16, 2009 5:26 AM
Hi Edson Nilo,

RowsAdded event will be fired when DataGridView populate rows from datasource.

>I need change some attributes like background color, rowheadertext contents, etc.

You can handle the DataBindingComplete event to set the background color and header text for the cells. For example
Set Row(0) Header text: dataGridView1.Rows[0].HeaderCell.Value = "Row1";
Set Color for Cell(0, 0): dataGridView1[0, 0].Style.BackColor = Color.Blue;

If you have anything unclear, please feel free to tell me.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
Kira Qian  Tuesday, September 15, 2009 7:12 AM
Hi Kira Qian,

Thanks for your answer.

The problem is that, I need change attributes before rows display, because colors, by example, depending on situation.

DataGridViewCellFormattingEventArgs

I use de event above, but I´m not sure if is the best way!

Thanks again!

Edson NIlo
Edson Nilo  Tuesday, September 15, 2009 12:05 PM
Hi Edson Nilo,

I think give you a sample can better explain my meaning. Please look at the following sample.
public partial class Form1 : Form
{
private DataTable dt = new DataTable();
private bool firstRun = true;

public Form1()
{
InitializeComponent();
dt.Columns.Add("Column");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(i);
}
dataGridView1.DataSource = dt;
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
}

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (firstRun)
{
foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
{
if (dgvRow.IsNewRow == false && dgvRow.Cells[0].Value.ToString() == "1")
{
dgvRow.Cells[0].Style.BackColor = Color.Red;
}
}
firstRun = false;
}
}

void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.FormattedValue.ToString() == "1")
{
dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Red;
}
}
}

In this sample, I will set the cell's backcolor to Red whose value is 1, So I handle the CellValidating event to do the check and set. But only handle that event is not enough. When the form is load, you can see the cell whose value is 1 doesn't have red backcolor. That's the reason why I handle DataBindingComplete event to set the color at the first time.

Do you understand my meaning?

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
  • Marked As Answer byEdson Nilo Wednesday, September 16, 2009 9:24 PM
  •  
Kira Qian  Wednesday, September 16, 2009 5:26 AM
Yes Kira,

Now I understood!

It works perfectly!

Thanks a lot.

Edson
Edson Nilo  Wednesday, September 16, 2009 9:24 PM

You can use google to search for other answers

Custom Search

More Threads

• Disabling a Control's Beep! [C#]
• How to re-authenticate user?
• costum controls with GDI+
• WebBrowser cant navigate to local file
• detailed listview questions
• Building an InfoPath style application
• What is the correct way to add a control to its container
• Auto size modification from English to Chinese Windows.
• Enter key problem
• How to avoid confirmation dialog in WebBrowser in designMode?