Reputation: 1360
I'm trying to create report and it can view in DataGridView
. I'm using a simple codes in viewing the values.
Here's my code when the view button is click:
belreport.DailyReport = Convert.ToDateTime(date_day_Daily.Text).ToString("yyyy-MM-dd");
DataTable table = balreport.ViewDailyRecord(belreport);
dgv_daily.DataSource = table;
Here's my code for viewing the data from the table in my database:
// START Executing to view the data
public DataTable ViewDailyRecord(BELReport belreport) {
SqlCommand cmd = new SqlCommand();
cmd.Connection = dbcon.getcon();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM table WHERE Date=@Date";
cmd.Parameters.AddWithValue("@Date",belreport.DailyReport);
SqlDataReader dr = cmd.ExecuteReader();
DataTable table = new DataTable();
table.Load(dr);
return table;
}
In my table the value can be duplicate but they have unique identifier. What I'm trying to do is something like this
When there's duplicate or multiple value of the Identifier, all you can see are the First Identifier and Particulars and amount(Because the Identifier looks multiple, I want to get rid of it)
Thanks in advance
Upvotes: 1
Views: 986
Reputation: 125247
DataGridView
doesn't have any built-in support to show rows in groups. For reporting purpose it's better to use reporting tools like rdlc
report.
But if the result which is shown in the image is an acceptable result, you can achieve that appearance by handling CellFormatting
event and setting formatted value of each cell, based on previous cell at the column:
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//I suppose the group column is column at index 0
if (e.ColumnIndex == 0 && e.RowIndex > 0)
{
if (dataGridView1[0, e.RowIndex].Value == dataGridView1[0, e.RowIndex - 1].Value)
e.Value = "";
}
}
Here is an example output:
To test it yourself:
private void Form1_Load(object sender, EventArgs e)
{
var dt = new DataTable();
dt.Columns.Add("C1", typeof(string));
dt.Columns.Add("C2", typeof(string));
dt.Rows.Add("1", "11");
dt.Rows.Add("2", "21");
dt.Rows.Add("3", "31");
dt.Rows.Add("1", "12");
dt.Rows.Add("2", "22");
dt.Rows.Add("3", "32");
dt.Rows.Add("1", "13");
dt.Rows.Add("2", "23");
this.dataGridView1.DataSource = dt;
this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
foreach (DataGridViewColumn column in this.dataGridView1.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
}
Upvotes: 2