Skip to content

How to sort serial no inside gridview

Narayanan Ramachandran

Asked Jun 7, 2013, 11:41 PM EDT1 answer
Share
LinkedInX
Hi

I am having datagridview in c#

I populated data inside datagridview.

I created Serial No inside datagridview along with other columns.

If i click serial no head,the serial no is sorted.
If i do sorting in other columns,the serial no is also sorted.

How to avoid sorting serial no, if I click other column headers.

I have given the code below



dsstock = new DataSet();
dsstock = fetchDataSetValues(statement);
// grdStock.Rows.Clear();
if (dsstock.Tables[0].Rows.Count > 0)
{
grdStock.Columns.Add("Sl. No.", "Sl. No.");
grdStock.Columns[0].Width = 80;

grdStock.Columns.Add("ItemID", "Item ID");
grdStock.Columns[1].Width =300;
grdStock.Columns.Add("ItemName", "Item Name");
grdStock.Columns[2].Width = 600;
grdStock.Columns.Add("ItemQuantity", "Item Quantity");
grdStock.Columns[3].Width = 150;
int row = dsstock.Tables[0].Rows.Count - 1;

for (int r = 0; r <= row; r++)
{
grdStock.Rows.Add();
grdStock.Rows[r].Cells[0].Value = r + 1;
grdStock.Rows[r].Cells[1].Value = dsstock.Tables[0].Rows[r].ItemArray[0];
grdStock.Rows[r].Cells[2].Value = dsstock.Tables[0].Rows[r].ItemArray[1];
grdStock.Rows[r].Cells[3].Value = dsstock.Tables[0].Rows[r].ItemArray[2];

}
}
Thanks

Chandran

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    0
    Hi Chandran,

    May be you're on the track with your requirement but believe me, this is totally illogical to have only one column sorted in a table. Suppose you would have sorted a single column without affecting the other ones then how could you identify what serial no. belongs to what Item. This would be completely illogical. So, in my opinion it's not possible in DataGridView.


    Hope it helps. Happy Coding :)

    Thanks.
    0