-2

我正在尝试增加在 c# 中创建的列的宽度,我的代码是,

DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("Project ID", typeof(int)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("name", typeof(double)));
dt.Columns.Add(new DataColumn("email", typeof(double)));
dt.Columns.Add(new DataColumn("description", typeof(double)));
dt.Columns.Add(new DataColumn("ownerdetail", typeof(double)));
DataRow dr;

如何设置它的宽度?我发现像这样实现它,

dt.Columns.Add(new DataColumn("Title", typeof(string))).width; 

但是“ .with ”不存在,那我们该如何解决呢?希望您的建议提前谢谢

编辑: 如何在网格视图中设置列存在我的代码是,

GridViewHcost.DataSource = dset;

我尝试将其作为来自网络的帮助,

GridViewHcost.Columns.width = 20;

但是在这个命令中“ .Width ”不存在。希望你的建议

4

7 回答 7

3

A DataTable is a non-UI class in the .Net framework. This means that there is no "width" for you to change.

So the answer is, you cannot do it.

If your DataTable is bound to a UI control, then you will need to look at modifying that control instead.

于 2013-06-07T09:54:29.633 回答
2

我有同样的问题,在将 dataTable 添加到您的 GridView 后,您可以像这样更改每个列的宽度:

GridView1.Rows[0].Cells[3].Width = 120;
于 2016-10-24T16:57:02.947 回答
1

您可以简单地将数据表与 datagridview 绑定。比如:dataGridView1.DataSource = dt; //dt 是数据表,然后计算数据表的列数,并使用 datagridview 为每列指定 cloumn 宽度。喜欢:

        dataGridView1.AutoGenerateColumns = true;
        DataGridViewColumn ID_Column = dataGridView1.Columns[0];
        ID_Column.Width = 200;

等等所有列..

于 2014-09-25T19:50:30.573 回答
0

然后你必须调整gridview gridview.columns[?].Width 我想或Properties.Width 可能。

于 2013-06-07T09:57:15.577 回答
0

Width does not exist in DataTable object. You can set the Width in a DataGrid or GridView. DataTable is a collection of rows and columns. That's all there is to it.

Both DataGrid and GridView should have a Columns property. If you know how many columns you have and you know exactly which one to extend in width, you can use:

var column = GridView.Columns[columnIndex];
column.Width = 150;

That's about it.

于 2013-06-07T09:54:39.103 回答
0

A datatable is used for in memory storage of data... that data is then typically bound to a gui control such as a dataview, the column of the dataview could then be widened.

Dataview.Width

于 2013-06-07T09:54:58.393 回答
0

A DataColumn has no width, do you mean restrict the number of characters in the column via a constraint or affect the display in some way?

If you mean the display, then it depends on how you are outputting the data I.E. If you are you data binding to a GridView, you can override the RowDataBound event and customise the output.

If you mean constrain the field size, then I don't think there is anything built in you can use. I tend to validate the input field length manually before inserting into a database.

于 2013-06-07T09:59:03.703 回答