How to sort data in ascending order in data table on particular column

Hi All,

How to sort data table in ascending order on particular column. There may be a possibility that this column contains a blank value. I don’t want to remove this blank value and neither its position (index).
Can you guys please help me achieving this.

Regards,
Paresh Borole


if it is in excel Just pass you datatable and column you want to sort ascending or descending.

else you can write inDataTable.Select(your column list)order by column name

1 Like

Hello @Paresh

You can try as @Divyashreem have suggest for you, or as example below:

dt.DefaultView.Sort = “[Column1] ASC, [Column2] ASC”
dt = dt.DefaultView.ToTable

4 Likes

Hi @Divyashreem and @dkollyns,

Thanks for your reply. I am preferring to go with -

dt.DefaultView.Sort = “[Column1] ASC, [Column2] ASC”
dt = dt.DefaultView.ToTable

I want to Pass Column1 As integer in this. How can i get that?

Thanks,
Paresh Borole

1 Like

dt.DefaultView.Sort = “[Column1] ASC, [Column2] ASC”
dt = dt.DefaultView.ToTable

in this [Column1] is a column name-> Convert.Toint32(iDataTable.Item(“Column1”).ToString) this will convert your column data to int format

2 Likes

Hi @Divyashreem,

Please help me with this syntax error.

Thanks,
Paresh Borole

Hello @Paresh

Try this:

dt2 = dt.clone
dt2.Columns(“Column1”).DataType = Type.GetType(“System.Int32”)
For Each dr As DataRow In dt.Rows
dt2.ImportRow(dr)
Next
dt2.AcceptChanges
dt = dt2.DefaultView.ToTable

2 Likes