开发者

2D Arrays in c#

开发者 https://www.devze.com 2023-03-09 01:09 出处:网络
I have a 2D array. string[,] my2DArr= new string[r,c]; string actual=\"header1,header2,header3\"; I want to achieve copying 开发者_JAVA技巧a row to zeroth row of 2d array:

I have a 2D array.

string[,] my2DArr= new string[r,c];

string actual="header1,header2,header3";

I want to achieve copying 开发者_JAVA技巧a row to zeroth row of 2d array:

my2DArr[0]=actual.Split(',');

GridView gv= new GridView();
gv.AutoGeneratedColumns=true;
gv.DataSource=my2DArr;
gv.DataBind();

This is not showing the data. any suggestions greatly appreciated. Thanks


Is there a reason why you can't work with lists?

List<List<string>> my2DArray = new List<List<string>>();
my2DArray.Add(actual.Split(',').ToList());

Update: When you want to bind it, you might need to use this code:

List<string[]> my2DArray = new List<string[]>();
my2DArray.Add(actual.Split(','));


To assign 2d array to gridview , here is code . To add a new row , you can add new ListItem("Header1", "Header2");

 ArrayList arrList = new ArrayList();
 arrList.Add(new ListItem("Header1", "Header2") );

               for (int i = 0; i < array.GetLength(0); i++)
               {

                   arrList.Add(new ListItem(array[i, 0], array[i, 1]));

               }

               GridView1.DataSource = arrList;

               GridView1.DataBind();  
0

精彩评论

暂无评论...
验证码 换一张
取 消