简体   繁体   中英

How do i get rid of the extra columns?

I just made a list of clients , got the clients details from the database, added them to a list, then added the list to the DataGrid but I get so many extra columns ! Here is my DataGrid xaml code :

<DataGrid  x:Name="dataGridC" HorizontalAlignment="Left"  Margin="10" VerticalAlignment="Top" Height="150" Width="1000" />

And this is my code-behind:

var cn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ASUS\Documents\Visual Studio 2015\Projects\G_D_C v2\G_D_C v2\G_D_C.mdb");
                cn.Open();

                OleDbCommand cmd = new OleDbCommand();


                cmd.CommandText = "select * from [client]";
                cmd.Connection = cn;

             //   OleDbDataReader rd = cmd.ExecuteReader();
                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {

                        while (reader.Read())
                        {
                            int ID = reader.GetInt32(0);
                            string nom_cs = reader.GetString(1);
                            int num_tel = reader.GetInt32(2);
                            int fax = reader.GetInt32(3);
                            string adresse = reader.GetString(4);

                            client c = new client(ID, nom_cs, num_tel, fax, adresse);
                            lsc.Add(c);
                        }
                    }

              //  dataGridC.ItemsSource = rd;


                  dataGridC.ItemsSource = lsc;

ScreenShot

<DataGrid AutoGenerateColumns="False" />

https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v=vs.110).aspx

Right because you didn't specify what you wanted to show up. AutoGenerateColumns="False" will not generate the columns that are present in your DataGrid.ItemSource because that property just told it not to generate any columns... the rows might show but nothing will actually be visible.

<DataGrid x:Name="Grid" AutoGenerateColumns="False">
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding Id}" Header="ID" />
      <DataGridTextColumn Binding="{Binding nom_cs}" Header="Nom CS" />
      <DataGridTextColumn Binding="{Binding num_tel}" Header="Num Tel" />
      <DataGridTextColumn Binding="{Binding fax}" Header="Fax" />
      <DataGridTextColumn Binding="{Binding Adresse}" Header="Adresse" />
    </DataGrid.Columns> 
</DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM