如何将一个DataSet转换成数组? 效果就像DataGrid.DataSource=ds一样能DataGrid.DataSource=数组名? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本来就可以的,只要实现了IEnumerable接口就可以做为DataGrid的DataSource,数组都是实现这个接口的。 <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"/> 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(); }}大概就是这个意思吧!!! 我不要赋值,我要的是转换: 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; }这样可以吗? 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; } 楼上的有错误:System.NullRefrenceException:未将对象引用设置到对象的实例. 我要将DataSet中的一个表转换成一个二维数组后绑定效果和原来一样,怎么没有人知道啊? 把DataSet转换成一个二维数组再绑定到DataGrid上?楼上这样做有什么意义呢?? 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(); asp.net 打开新窗口的方法,window.open 会被IE 禁用 Button 单击了一次后. 按f5刷新又被执行一次? 新闻内容页跟新闻列表页应该怎么做?(c#) 请问子类如何发布父类的成员方法? 访问父页面函数? ASP.net 简单问题复杂化! nant--使用过的人进来指导下--50分 如何获得配置文件的绝对路径 怎样当用户一登入页面后自动与后台数据库定时,完成时间同步! 请大家帮一下忙 sql(在线等) 关于在asp.net中连接数据库的问题!
if(!Page.IsPostBack){
string[] names={"one","two","three","four","five","six","seven","eight"};
grid.DataSource=names;
grid.DataBind();
}
<asp:DataGrid runat="server" id="grid"/>
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();
}
}
大概就是这个意思吧!!!
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;
}
这样可以吗?
{
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;
}
System.NullRefrenceException:未将对象引用设置到对象的实例.
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();