如何将Object [,]绑定到datagridview? [英] How to bind Object[,] into datagridview?

查看:119
本文介绍了如何将Object [,]绑定到datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我如何将其绑定到数据网格视图?



或将其转换为数据View或BindingList或DataTable,然后将其绑定到datagridview?



编辑:
对象[ ,]是完全动态的数据(不同的行,列,数据类型,像转储数据):(Excel表)



[0,x]kjslwe3 w45erer643reew5456 34



[1,y] 23e1sf123213ds343433

解决方案

您没有使用



在上面的例子中,我还设置了这些属性:

  grid.AllowUserToAddRows = false; 
grid.AllowUserToDeleteRows = false;
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;


I have 2D array with different types of data(int, string, float).

How do i bind it to data grid view?

Or converting it to data View or BindingList or DataTable and then bind it to datagridview?

Edits: Object[,] is completely dynamic data(Different no of rows, columns, datatypes., Like dump data): (Excel sheets)

[0,x] "kjslwe" 3 "w" 45 "erer" 643 "reew" "54" 56 34

[1,y] 23 "e" 1 "sf" 123213 "ds" 343433

解决方案

You are not using a 2D Array, you are using a Jagged Array. A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."

To show a jagged array in DataGridView, you can first calculate the number of columns which you need and set ColumnCount property of grid. Then add rows using an overload of Add method of Rows collection of grid which accepts param object[]. For example:

object[][] data = new object[][]{ 
        new object[] {"kjslwe", 3, "w", 45, "erer", 643, "reew", "54", 56, 34},
        new object [] {23, "e", 1, "so", 123213, "ds", 343433}
};

var columns = data.Max(x => x.Count());         /* Calculate number of columns */
grid.ColumnCount = columns;                     /* Set column count of grid   */
data.ToList().ForEach(x => grid.Rows.Add(x));   /* Add rows                    */

In the above example, I also set these properties:

grid.AllowUserToAddRows = false;
grid.AllowUserToDeleteRows = false;
grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;

这篇关于如何将Object [,]绑定到datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆