ds.Tables[0].Rows[0][0]取不到第一行第一列
在asp.net里面可以啊,怎么到了cs上面就不可以用了
怎么取某行某列的值.注ds.Tables[0].Rows[0][0]取不到第一行第一列
9 个解决方案
#1
这?你还是检查检查自己的代码吧,不可能出现这种情况的.
#2
数据是什么,调试
#3
要不试试这个:ds.Tables[0].Rows[0].Cells[0].Value;
#4
只为学习不为分
#5
先输出一下ds.Tables[0]的行数,看看是否取到了数据,再试!
#6
DataTable里有数据吗??
#7
语法没问题,应该是你根本没绑定到datatable中,调试一下,看看行数多少
#8
语法没有错啊 看不出来你那写错了;可能你的数据第一行为空吧?你好好看看你的输出数据
#9
DataSet ds1;
public DataSet fillds_returnds()
{
string constr = " Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
SqlConnection cn = new SqlConnection(constr);
string cmd = " select top 10 * from Products";
SqlDataAdapter da = new SqlDataAdapter(cmd, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
return ds;
}
private void Form1_Load(object sender, EventArgs e)
{
ds1= fillds_returnds();
}
private void button2_Click(object sender, EventArgs e)
{
ds1 = fillds_returnds();
//方法2
XmlTextWriter xmlw = new XmlTextWriter("xxxxxx.xml",Encoding.UTF8);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("Products"); //<Products>
for (int i = 0; i <= ds1.Tables[0].Rows.Count-1; i++)
{
// <Product PID="1">
xmlw.WriteStartElement("Product");
xmlw.WriteAttributeString( "PID", ds1.Tables[0].Rows[i].ItemArray[0].ToString());//奇怪的是这句话倒可以但是ds1.Tables[0].Rows[0]取不到值
//<Name>Chai</Name>
xmlw.WriteStartElement("Name");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[1].ToString());
xmlw.WriteEndElement();
// <Price>18.0000</Price>
xmlw.WriteStartElement("Price");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[5].ToString());
xmlw.WriteEndElement();
// </Product>
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
xmlw.Close();
//导入TextBox
StreamReader sr = new StreamReader("xxxxxx.xml");
this.textBox1.Text = sr.ReadToEnd();
//MessageBox.Show("成功");
}
public DataSet fillds_returnds()
{
string constr = " Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
SqlConnection cn = new SqlConnection(constr);
string cmd = " select top 10 * from Products";
SqlDataAdapter da = new SqlDataAdapter(cmd, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
return ds;
}
private void Form1_Load(object sender, EventArgs e)
{
ds1= fillds_returnds();
}
private void button2_Click(object sender, EventArgs e)
{
ds1 = fillds_returnds();
//方法2
XmlTextWriter xmlw = new XmlTextWriter("xxxxxx.xml",Encoding.UTF8);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("Products"); //<Products>
for (int i = 0; i <= ds1.Tables[0].Rows.Count-1; i++)
{
// <Product PID="1">
xmlw.WriteStartElement("Product");
xmlw.WriteAttributeString( "PID", ds1.Tables[0].Rows[i].ItemArray[0].ToString());//奇怪的是这句话倒可以但是ds1.Tables[0].Rows[0]取不到值
//<Name>Chai</Name>
xmlw.WriteStartElement("Name");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[1].ToString());
xmlw.WriteEndElement();
// <Price>18.0000</Price>
xmlw.WriteStartElement("Price");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[5].ToString());
xmlw.WriteEndElement();
// </Product>
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
xmlw.Close();
//导入TextBox
StreamReader sr = new StreamReader("xxxxxx.xml");
this.textBox1.Text = sr.ReadToEnd();
//MessageBox.Show("成功");
}
#1
这?你还是检查检查自己的代码吧,不可能出现这种情况的.
#2
数据是什么,调试
#3
要不试试这个:ds.Tables[0].Rows[0].Cells[0].Value;
#4
只为学习不为分
#5
先输出一下ds.Tables[0]的行数,看看是否取到了数据,再试!
#6
DataTable里有数据吗??
#7
语法没问题,应该是你根本没绑定到datatable中,调试一下,看看行数多少
#8
语法没有错啊 看不出来你那写错了;可能你的数据第一行为空吧?你好好看看你的输出数据
#9
DataSet ds1;
public DataSet fillds_returnds()
{
string constr = " Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
SqlConnection cn = new SqlConnection(constr);
string cmd = " select top 10 * from Products";
SqlDataAdapter da = new SqlDataAdapter(cmd, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
return ds;
}
private void Form1_Load(object sender, EventArgs e)
{
ds1= fillds_returnds();
}
private void button2_Click(object sender, EventArgs e)
{
ds1 = fillds_returnds();
//方法2
XmlTextWriter xmlw = new XmlTextWriter("xxxxxx.xml",Encoding.UTF8);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("Products"); //<Products>
for (int i = 0; i <= ds1.Tables[0].Rows.Count-1; i++)
{
// <Product PID="1">
xmlw.WriteStartElement("Product");
xmlw.WriteAttributeString( "PID", ds1.Tables[0].Rows[i].ItemArray[0].ToString());//奇怪的是这句话倒可以但是ds1.Tables[0].Rows[0]取不到值
//<Name>Chai</Name>
xmlw.WriteStartElement("Name");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[1].ToString());
xmlw.WriteEndElement();
// <Price>18.0000</Price>
xmlw.WriteStartElement("Price");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[5].ToString());
xmlw.WriteEndElement();
// </Product>
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
xmlw.Close();
//导入TextBox
StreamReader sr = new StreamReader("xxxxxx.xml");
this.textBox1.Text = sr.ReadToEnd();
//MessageBox.Show("成功");
}
public DataSet fillds_returnds()
{
string constr = " Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
SqlConnection cn = new SqlConnection(constr);
string cmd = " select top 10 * from Products";
SqlDataAdapter da = new SqlDataAdapter(cmd, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0];
return ds;
}
private void Form1_Load(object sender, EventArgs e)
{
ds1= fillds_returnds();
}
private void button2_Click(object sender, EventArgs e)
{
ds1 = fillds_returnds();
//方法2
XmlTextWriter xmlw = new XmlTextWriter("xxxxxx.xml",Encoding.UTF8);
xmlw.Formatting = Formatting.Indented;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("Products"); //<Products>
for (int i = 0; i <= ds1.Tables[0].Rows.Count-1; i++)
{
// <Product PID="1">
xmlw.WriteStartElement("Product");
xmlw.WriteAttributeString( "PID", ds1.Tables[0].Rows[i].ItemArray[0].ToString());//奇怪的是这句话倒可以但是ds1.Tables[0].Rows[0]取不到值
//<Name>Chai</Name>
xmlw.WriteStartElement("Name");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[1].ToString());
xmlw.WriteEndElement();
// <Price>18.0000</Price>
xmlw.WriteStartElement("Price");
xmlw.WriteString(ds1.Tables[0].Rows[i].ItemArray[5].ToString());
xmlw.WriteEndElement();
// </Product>
xmlw.WriteEndElement();
}
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
xmlw.Close();
//导入TextBox
StreamReader sr = new StreamReader("xxxxxx.xml");
this.textBox1.Text = sr.ReadToEnd();
//MessageBox.Show("成功");
}