mark333...333...333
mark333...333...333

Reputation: 1360

C# WinForm DataGrid CellFormatting Does Not Work In Datasource Issues

Good day, I'm showing my queries from table in to the datagrid. But I'm having a problem with my codes. I tried to debug and add break point in my dgview_CellFormatting but the my code doesn't go to the event handler of cell formatting. I don't know why.

Here's my code. I put my datasource in my Form load

// dgv_details is supposed to be my datagrid view name.

DataTable table;

table = balreport.SelectDetails(belreport); // responsible for fetching data from the database.

this.dgv_details.DataSource = table;

this.dgv_details.Sort(this.dgv_details.Columns[0], ListSortDirection.Ascending);
// For sorting the data
foreach (DataGridViewColumn column in this.dgv_details.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.NotSortable;
}

this.dgv_details.CellFormatting += dgv_details_CellFormatting;

And here's my dgv_details_CellFormatting event handler

 if (e.ColumnIndex == 0 && e.RowIndex > 0)
 {
     if (dgv_salesdetails[0, e.RowIndex].Value == dgv_salesdetails[0, e.RowIndex - 1].Value)
     e.Value = "";
 }

The sorting is working properly, but the problem is, the e.Value should be hide. The result should be something like this image

enter image description here

I really need a help.

Upvotes: 1

Views: 546

Answers (1)

I A Khan
I A Khan

Reputation: 8839

use this

// dgv_details is supposed to be my datagrid view name.
this.dgv_details.CellFormatting += dgv_details_CellFormatting;
DataTable table;

table = balreport.SelectDetails(belreport); // responsible for fetching data from the database.

this.dgv_details.DataSource = table;

this.dgv_details.Sort(this.dgv_details.Columns[0], ListSortDirection.Ascending);
// For sorting the data
foreach (DataGridViewColumn column in this.dgv_details.Columns)
{
    column.SortMode = DataGridViewColumnSortMode.NotSortable;
}

cause DataGrid CellFormatting event Does Work when datagrid fill.

Upvotes: 1

Related Questions