效果就像DataGrid.DataSource=ds一样能DataGrid.DataSource=数组名?

解决方案 »

  1.   

    本来就可以的,只要实现了IEnumerable接口就可以做为DataGrid的DataSource,数组都是实现这个接口的。
      

  2.   

    <script runat="server">void Page_Load(object src,EventArgs e){
    if(!Page.IsPostBack){
    string[] names={"one","two","three","four","five","six","seven","eight"};
    grid.DataSource=names;
    grid.DataBind();
    }
    <asp:DataGrid runat="server" id="grid"/>
      

  3.   

    object[][] str=null;
    for(int i=0;i<ds.Tables[0].Rows.Count;i++)
    {
       for(int j=0;j<ds.Tables[0].Columns.Count;j++)
       {
    str[i][j]=ds.Tables[0].Rows[i][j].ToString();
       }
    }
    大概就是这个意思吧!!!
      

  4.   

    我不要赋值,我要的是转换:
    public ArrayList ArrayList_Northwind_customers()
    {
    DataSet ds=new DataSet();
    ArrayList al=new ArrayList();
    SqlConnection conn=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
    SqlDataAdapter da=new SqlDataAdapter("select top 100 CustomerID,CompanyName,ContactName,ContactTitle from customers",conn);
    conn.Open();
    da.Fill(ds,"Northwind_customers");
    foreach(DataRow row in ds.Tables["Northwind_customers"].Rows)
    {
    foreach(System.Data.DataColumn col in ds.Tables["Northwind_customers"].Columns)
    {
    al.Add(row[col].ToString());
    }
    }
    conn.Close();
    return al;
    }
    这样可以吗?
      

  5.   

    private Array ArrayList_Northwind_customers()
    {
    DataSet ds=new DataSet();
    ArrayList al=new ArrayList();
    SqlConnection conn=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=Northwind");
    SqlDataAdapter da=new SqlDataAdapter("select top 100 CustomerID,CompanyName,ContactName,ContactTitle from customers",conn);
    conn.Open();
    da.Fill(ds,"Northwind_customers");
    object[][] str=null;
    for(int i=0;i<ds.Tables["Northwind_customers"].Rows.Count;i++)
    {
    for(int j=0;j<ds.Tables["Northwind_customers"].Columns.Count;j++)
    {
    str[i][j]=ds.Tables["Northwind_customers"].Rows[i][j].ToString();
    }
    }
    conn.Close();
    return str;
    }
      

  6.   

    楼上的有错误:
    System.NullRefrenceException:未将对象引用设置到对象的实例.
      

  7.   

    我要将DataSet中的一个表转换成一个二维数组后绑定效果和原来一样,怎么没有人知道啊?
      

  8.   

    把DataSet转换成一个二维数组再绑定到DataGrid上?楼上这样做有什么意义呢??
      

  9.   

    SqlConnection conn=new SqlConnection("server=.;uid=sa;pwd=;database=pubs");
    conn.Open();
    string strSQL="select au_lname,au_fname,address from authors";
    DataRow drName;
    DataTable dtStuUsers=new DataTable("authors");
    DataSet dsStudent=new DataSet();
    dsStudent.Tables.Add(dtStuUsers);
    int intCount=3;
    SqlDataReader sdrStudent;
    SqlCommand cmdStudent;
                               //二维数组的行和列(SQL语句所选择的烈数)自己定义(我是根据表中的记录数来定的)
    String[,] strText=new String[23,3];
    int intI=0,intJ=0;
    for(int i=0;i<intCount;i++)
    {
    //创建表中的列
    dtStuUsers.Columns.Add(new DataColumn("列"+i.ToString(),typeof(string)));
    }
    //打开数据集
    cmdStudent=new SqlCommand(strSQL,conn);
    sdrStudent=cmdStudent.ExecuteReader();
    //向内存表中添加数据
    while(sdrStudent.Read())
    {
    drName=dtStuUsers.NewRow();
    for(intI=0;intI<3;intI++)
    {
    strText[intJ,intI]=sdrStudent.GetString(intI).ToString();
    //你可以将数组操作以后再将数值填充到 drName里,在绑到DataGrid里
    drName[intI]=sdrStudent.GetString(intI);
    }
    intJ=intJ+1;
    dtStuUsers.Rows.Add(drName);
    }
    for(intJ=0;intJ<23;intJ++)
    {
    for(intI=0;intI<3;intI++)
    {
    Response.Write(strText[intJ,intI]+"**");
    }
    Response.Write("<br>");
    }
    dgName.DataSource=dsStudent.Tables["authors"];
    dgName.DataBind();