Reputation: 15
I am doing a winforms application in C#.net I have a set of data in a grid view (populated based on textbox entry. If I enter ID in textbox, its corresponding data in grid view is shown as follows:
A B C D
100 1 30500 null
100 1 23000 null
100 1 50000 null
100 2 23000 null
100 2 31300 null
100 2 50000 null
In the above data, the value 50000 in column C has 2 sub items each corresponding to the value in column B(30500,23000-> value 1 in B and 23000,31300-> value 2 in column B). I would like to see the table as follows.:
A B C D
100 1 50000 null
100 1 30500 null
100 1 23000 null
100 2 50000 null
100 2 23000 null
100 2 31300 null
I would like to have the row corresponding to the value 50000( highest) to appear first to show that the below 2 entries (beneath each 50000) are its sub items.
I do not know how to do that since I am a beginner in c# Winforms.
Appreciate any help. Thanks in advance.
.
Upvotes: 0
Views: 103
Reputation: 811
You have to sort the DataSource that is being used for your datagridView. I am assuming that a datatable is the data source for you. you have to apply sort twice one by column B as ascending then Column C as descending.
if dataTable1 is your datasource then somrthing similar like the below you need to do ..
DataView view = new DataView(dataTable1);
view.Sort = "B ASC, C DESC";
DataTable newTable = view.ToTable();
and then use NewTable As your data source.
Other way is to handle from the DB side by adding ORDER BY B ASC, C DESC at the end of your current query.
Upvotes: 1
Reputation: 2376
The answer by kishore V M will work if you have a datatable as your data source, here is another way to do it that works regardless of the data source.
Register on the DataGrid SortCompare and handle the sort comparison yourself.
dgv.SortCompare += new DataGridViewSortCompareEventHandler(OnDataGridViewSortCompare);
void OnDataGridViewSortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
int retVal = String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString());
//They are ==, Compare by next column
if (retVal == 0)
{
retVal = String.Compare(dgv[e.Column.Index + 1, e.RowIndex1].Value.ToString(),
dgv[e.Column.Index + 1, e.RowIndex2].Value.ToString()) * -1; //Multiply by -1 to flip the ASC sort to DESC
}
e.SortResult = retVal;
e.Handled = true;
}
I am unsure if you are storing strings or int values so i used string compare. If you want a true numeric compare you need to cast/parse the cell values to int and use your own int compare method since int does not have one built in like string or decimal.
void OnDataGridViewSortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
int retVal = CompareInt((int)e.CellValue1, (int)e.CellValue2);
//They are ==, Compare by next column
if (retVal == 0)
{
retVal = CompareInt((int)dgv[e.Column.Index + 1, e.RowIndex1].Value,
(int)dgv[e.Column.Index + 1, e.RowIndex2].Value) * -1; //Multiply by -1 to flip the ASC sort to DESC
}
e.SortResult = retVal;
e.Handled = true;
}
int CompareInt(int value1, int value2)
{
if (value1 == value2) return 0;
else if (value1 < value2) return -1;
else return 1;
}
Upvotes: 0