0
我已经使用了DataRow,它可以显示在我的WinForm的组合框中,但不知何故,它不起作用,尽管整个表格已成功显示在gridview中。DataRow没有显示出来
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
con.ConnectionString = "Data Source=.;Initial Catalog=StudentDetails;Integrated Security=True";
cmd.Connection = con;
cmd.CommandText = "select * from StuDet";
da.SelectCommand = cmd;
da.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Rows.Count; i++)
{
comboBox1.Items.Add(dr[0]);
}
}
我可以使用“for循环”而不是foreach吗? – androidrill
a)是的,你可以,但你需要在你的循环中获得该行'DataRow dr = dt.Rows [i]'b)'foreach'有什么问题? – dkozl
我是一名学生,这就是为什么我要尝试各种各样的事情。我本来可以简单地在DataTable上工作并获得了组合框,但我仍然选择使用DataRow来学习一些概念。如果你发现我的答案有用,请给我投票。 – androidrill