blah blah blah is here! blah blah » Close

up0down
link

Hi there,

I know there's no RowDataBound event for the WinForms DataGridView. But there is CellFormatting event in WinForms DataGridView. So Cellformatting event is very very slow.

Is there any solutions like RowDataBound event in WinForms DataGridView (Other then Cellformatting) ?

Thanks for all.

last answered one year ago

5 answers

up0down
link

Well, as far as I can see, CellFormatting is the only event of the DataGridView which is remotely similar to the RowDataBound event of the GridView, so I don't think there are any other options.

It's fired each time a cell is repainted and so does tend to be slow.

However, there are a couple of 'common sense' performance tips in the MSDN documentation:

1. Avoid lengthy processing in the eventhandler.

2. Always access the cell through the DataGridViewCellFormattingEventArgs parameter rather than directly.

gsvirdi
412

Not only this... I found that I was trying to copy thre cells from Excel into three cells of my DGV, and this Darn thing will not accept it. The DGV is does not allows Paste.

up0down
link

Its a bullshit! How there isnt any other options. Only i wanna do like this;

a cell value is 0 or 1 (its coming database)

If the value is "0" grid should be write "yes" (it doesnt see 0)
If the value is "1" grid should be write "no" (it doesnt see 1)

I hope i was explained it.

Thanks.

up0down
link

Well, I don't know how you're doing it at the moment but, if the cells are in the 4th column (i.e. ColumnIndex == 3), I'd suggest you handle the CellFormatting event as follows:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 3 && e.Value != null)
{
if (e.Value.ToString() == "0")
e.Value = "yes";
else if (e.Value.ToString() == "1") // EDIT added 'if' clause
e.Value = "no";
e.FormattingApplied = true;
}
}

up0down
link

Another alternative is change it in the query that pulls from the database or in the original data table.

I'm not sure how you are getting the data or what you are doing with it, but if it is sql you can modify the select statement to return Yes or No instead of 1 or zero

Select Case When Myfield = 0 then 'Yes' else 'No' as CellValue from MyTable

Or depending on the table size, you might try to loop through the data table before you bind it to the datagridview. that way you are only updating it one time all at once not every time you repaint each cell.

up0down
link

Youre right @spotsknight. I can modify the select statement to return Yes or No instead of 1 or zero to SQL string. But i cant do it. Its a complicated and mixed form.

my main SQL string is;

SELECT tarihler.id as tarihler_id, tarihler.ajanda_tarih, tarihler.ajanda_saat, CAST(CONCAT(tarihler.ajanda_tarih, ' ',tarihler.ajanda_saat) AS DATETIME) as ajanda_full_tarih,'' as kisi,'' as konu,'' as yer,'' as notlar,'' as sil FROM tarihler,ajanda USE INDEX (sil) WHERE tarihler.sil='0' " + kisiler_id_SQL + " " + konular_id_SQL + " " + yerler_id_SQL + " " + iki_tarih_arasi_SQL + " and ajanda.tarihler_id=tarihler.id ORDER BY ajanda_full_tarih DESC LIMIT " + simdiki_sayfa + "," + kacar_gosterilecek + "


And then my formatting is;

private void DataGridView1Fromat(object sender, DataGridViewCellFormattingEventArgs e)
{

if (TarihlerGrid.Columns[e.ColumnIndex].Name == "sira_no")
{

DataGridView1.Rows[e.RowIndex].Cells["sira_no"].Value = e.RowIndex + 1;
DataGridView1.Rows[e.RowIndex].Cells["sil"].Value = "[x]";

string SQL1 = "SELECT kisiler_id FROM ajanda USE INDEX (tarihler_id) WHERE tarihler_id='" + DataGridView1.Rows[e.RowIndex].Cells["tarihler_id"].Value.ToString() + "' LIMIT 1";
string kisiler_id = Class.Fonksiyonlar.MySQL.ExecuteScalar(SQL1);

if (kisiler_id != "0")
{
string SQL2 = "SELECT * FROM kisiler USE INDEX (id) WHERE id='" + kisiler_id + "' LIMIT 1";
DataSet ds = Class.Fonksiyonlar.MySQL.DataSetGetir(SQL2, kisiler_id);

if (ds.Tables[0].Rows[0]["firma"].ToString() != "")
{
DataGridView1.Rows[e.RowIndex].Cells["kisi"].Value = ds.Tables[0].Rows[0]["ad_soyad"] + " - " + ds.Tables[0].Rows[0]["firma"];
}
else
{
DataGridView1.Rows[e.RowIndex].Cells["kisi"].Value = ds.Tables[0].Rows[0]["ad_soyad"];
}

}
}


My new opinion in this issue. I can use SQL View (and i can format my table how i want it. It will be fast then DataGridViewCellFormatting event)

I hope i was explained it.
Thanks for all.

Feedback