string 2维数组 str[int1,int2] int1,int2都是变量 如何捆到datagrid
有人说datatable 但是 datacolumn这么捆绑 
因为int1,int都是变量,要做2次循环 datacolumn在循环里这么赋值?
能给段代码不 思归在的 教教我啊 都问了 好几天了

解决方案 »

  1.   

    给你个例子:
    private void button1_Click(object sender, System.EventArgs e)
    {
    int rows = Int16.Parse(textBox1.Text);//行数
    int cols = Int16.Parse(textBox2.Text);//列数
    string[][] data = new string[rows][];
    int i, j;
    for(i = 0 ; i < rows ; i ++)
    {
    data[i] = new string[cols];
    for(j = 0 ; j < cols ; j ++)
    {
    data[i][j] = i.ToString() + "," + j.ToString();
    }
    }
    DataTable dt = new DataTable();
    for(j = 0 ; j < cols ; j ++)
    {
    dt.Columns.Add(j.ToString(), typeof(string));
    }
    for(i = 0 ; i < rows ; i ++)
    {
    dt.Rows.Add(data[i]);
    }
    dataGrid1.DataSource = dt;
    }
      

  2.   

    string[,] str=new string[6,2]{{"1","2"},{"3","4"},{"5","6"},{"7","8"},{"9","10"},{"11","12"}};
    DataTable dTable=new DataTable() ;
    DataRow dRow;
    dTable.Columns.Add(new DataColumn("one", typeof(Int16)));
    dTable.Columns.Add(new DataColumn("two", typeof(Int16)));for(int i=0;i<str.Length/2;i++)
    {
    dRow = dTable.NewRow();
    dRow["one"]=str[i,0];
    dRow["two"]=str[i,1];
    dTable.Rows.Add(dRow);
    }
    dataGrid1.DataSource=dTable.DefaultView;