简体   繁体   English

C#DataSet到DataGrid

[英]c# DataSet To DataGrid

I would like to convert my dataset which contains one table into a datagrid in order to get the width of each column to add many groups title with the correct width just above. 我想将包含一个表的数据集转换为一个数据网格,以获取每一列的宽度,以在上面添加具有正确宽度的许多组标题。

I've tried " mydatagrid.ItemsSource = mydataset.Table[0].defaultview;" 我已经尝试过“ mydatagrid.ItemsSource = mydataset.Table [0] .defaultview;” and it works properly except this instruction doesn't fill any columns in my datagrid so i can't get any width of any columns. 并且它正常工作,除了该指令不会填充我的数据网格中的任何列,因此我无法获得任何列的任何宽度。

If anyone have an idea, thanks a lot. 如果有人有想法,请多谢。

Have you set AutoGenerateColumns = true ? 您是否设置了AutoGenerateColumns = true

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx

Your question sounds a bit strange. 您的问题听起来有些奇怪。 You DO know, that DataGrid is a web control, yes? 您知道吗,DataGrid是一个Web控件,是吗?

First of all, there are two DataGrid controls: one in System.Windows.Forms namespace for Windows Forms and the other in System.Web.UI.WebControls for Web. 首先,有两个DataGrid控件:一个在Windows窗体的System.Windows.Forms命名空间中,另一个在Web的System.Web.UI.WebControls中。

In either case, DataGrid is a control that shows a data from a data source in a grid. 无论哪种情况,DataGrid都是一个控件,它显示网格中来自数据源的数据。 In order to show the data, you have to bind it to a control. 为了显示数据,您必须将其绑定到控件。

This is quote from DataGrid article: "To display a table in the System.Windows.Forms.DataGrid at run time, use the SetDataBinding method to set the DataSource and DataMember properties to a valid data source." 这是从DataGrid文章引用的:“要在运行时在System.Windows.Forms.DataGrid中显示表,请使用SetDataBinding方法将DataSource和DataMember属性设置为有效数据源。”

dataGrid1.SetDataBinding(SuppliersProducts, "Suppliers");

So i'm back with a solution. 所以我回来了。 My columns were empty cause my code was define before the event "Loaded" happen so now everything is perfect. 我的列是空的,因为我的代码是在事件“ Loaded”发生之前定义的,所以现在一切都非常完美。

Thanks for precision about the datagrid. 感谢您对datagrid的准确性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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