Hi,
I have a datagridview with many columns.I want to give this GridView Columns proper order according to my requirement.
Like example.
I have three columns
1.RegNo.
2.Name.
3.Address.

I want that the Name should be the first Column,RegNo second and Adress Third.
My application is in Silverlight Coded in C#.Net.
Please guide.
Thanks in advance

Dani AI

Generated

Short answer: the code showed works for WinForms DataGridView, but in Silverlight the DataGrid.Columns collection does not support a string indexer (so Columns["RegNo"] will fail). You can either control column order from the source (as suggested) or reorder the grid’s columns after they exist by setting each column’s DisplayIndex. For Silverlight do that by locating the column (by Header or by its bound property) and assigning DisplayIndex with an integer index.

Run this after the columns are generated (for example after you set ItemsSource or in the control's Loaded/auto-generated-column event):

for (int i = 0; i < myDataGrid.Columns.Count; i++)
{
    var header = (myDataGrid.Columns[i].Header ?? string.Empty).ToString();
    if (header == "Name")
        myDataGrid.Columns[i].DisplayIndex = 0;
    else if (header == "RegNo")
        myDataGrid.Columns[i].DisplayIndex = 1;
    else if (header == "Address")
        myDataGrid.Columns[i].DisplayIndex = 2;
}

If you prefer a declarative approach, disable auto-generation and declare columns in XAML in the order you want:

<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
    <DataGridTextColumn Header="RegNo" Binding="{Binding RegNo}" />
    <DataGridTextColumn Header="Address" Binding="{Binding Address}" />
  </DataGrid.Columns>
</DataGrid>

Troubleshooting tips: set DisplayIndex only after columns exist; use zero-based indexes; avoid assigning duplicate indexes while reordering (assign unique values or set them in one pass). This should resolve the error you saw, , and preserve the advice from and in the context of Silverlight.

Recommended Answers

All 6 Replies

Based on the Select query you wrote the columns will come .
for example write in this way : select Regno,Name,Adress,Pincode from temptable
this may gives you correct result

You can set tje order of the columns like:

dataGridView1.Columns["Name"].DisplayIndex = 0;
    dataGridView1.Columns["RegNo"].DisplayIndex = 1;
    dataGridView1.Columns["Adress"].DisplayIndex = 2;
commented: ++ +15

Hi Bonca,
thanks fr ur rply...
but when i use the above code and give name to columns .Columns["RegNo"].DisplayIndex = 0;
its showing error as this.[int] has some invalid argument rather when i use
.Columns[0].DisplayIndex = 0; ,this means it only takes int type...
please suggest...

I have wrote my query tat way only...in sequence to what i want...

Did you sort all the columns? if you change one, you have to re-order all the others. So that two columns wont have same columnIndex maybe.

yes i have sorted the columns...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.