如何去掉多余的列?

时间:2022-08-09 23:51:08

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中,但是我得到了很多额外的列!下面是我的DataGrid xaml代码:

<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

截图

2 个解决方案

#1


0  

<DataGrid AutoGenerateColumns="False" />

< DataGrid AutoGenerateColumns = " False " / >

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

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.

对,因为你没有指定你想要显示的内容。AutoGenerateColumns="False"不会生成DataGrid中存在的列。因为这个属性告诉它不要生成任何列…行可能会显示,但实际上什么也看不到。

<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>

#2


0  

I already tried it but than I get no column :/ ScreenShot of the result

我已经试过了,但是我没有看到任何专栏:/屏幕截图

Thanks a lot it worked :D

非常感谢,它成功了:D

#1


0  

<DataGrid AutoGenerateColumns="False" />

< DataGrid AutoGenerateColumns = " False " / >

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

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.

对,因为你没有指定你想要显示的内容。AutoGenerateColumns="False"不会生成DataGrid中存在的列。因为这个属性告诉它不要生成任何列…行可能会显示,但实际上什么也看不到。

<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>

#2


0  

I already tried it but than I get no column :/ ScreenShot of the result

我已经试过了,但是我没有看到任何专栏:/屏幕截图

Thanks a lot it worked :D

非常感谢,它成功了:D