如何使用数据表(数据集)填充Datagridview? [英] How Do I Fill Datagridview Using Datatable (Dataset)?
本文介绍了如何使用数据表(数据集)填充Datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的DataSet返回5行,包含5列数据。但是当我尝试在DataTable的循环中填充datagridview时,结果datagridview以单行显示所有表数据。像级联效应的东西。这是我的代码:
My DataSet is returning 5 rows with 5 columns of data. But when i tries to fill the datagridview in loop of DataTable, resultant datagridview shows all table data in single row. Something like cascading effect. Here is my code:
foreach (DataTable table in (GetSeatLayout(1).Tables)
{
foreach (DataRow row in table.Rows)
{
string[] arrRow = new string[] { row["Seat_Row_Name"].ToString() };
gvSeatLayout.Rows.Add(arrRow);
foreach (DataColumn column in table.Columns)
{
DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
// If Seat# from layout equals to booked seat#
if (isThisSeatBookedAlready(Convert.ToInt32(row[column].ToString()), 1, Convert.ToDateTime(journeyDtPara)))
{
btn1.FlatStyle = FlatStyle.Popup;
btn1.Width = 25;
btn1.HeaderText = "Seat";
btn1.Text = row[column].ToString();
btn1.Name = "btn1_" + row[column].ToString();
btn1.DefaultCellStyle.BackColor = Color.Gray;
btn1.UseColumnTextForButtonValue = true;
gvSeatLayout.Columns.Add(btn1);
}
else
{
btn1.FlatStyle = FlatStyle.Popup;
btn1.Width = 25;
btn1.HeaderText = "Seat";
btn1.Text = row[column].ToString();
btn1.Name = "btn1_" + row[column].ToString();
btn1.UseColumnTextForButtonValue = true;
gvSeatLayout.Columns.Add(btn1);
}
}
}
}
}
}
推荐答案
为什么每次轮回时都要添加列?
想想你在做什么:
对于每一行,你看一下原来的每一列表,并在输出表中添加另一列。
因此,如果原始表有2列,3行:
第一行:添加一行,两列to gvSeatLayout
第二行:添加第二行,并向gvSeatLayout添加另外两列
第三行:添加第三行,并向gvSeatLayout添加另外两列
最后,gvSeatLayout以6列结束,只有第一列获得任何数据。
我有不知道你在这里想做什么,但我认为你需要坐下来思考你想要展示的确切内容,以及你想要显示哪些数据 - 因为没有它你只是在猜测该做什么,没有人可以真正帮助你!
Why are you adding columns each time you go round?
Think about what you are doing:
For each row, you look at each column in the original table, and add another column to teh output table.
So if your original table had 2 columns, and 3 rows:
First row: add one row, and two columns to gvSeatLayout
Second row: add second row, and add another two columns to gvSeatLayout
Third row: add third row, and add another two columns to gvSeatLayout
At the end, gvSeatLayout ends up with 6 columns, and only the first ever gets any data.
I have no idea what you are trying to do here, but I think you need to sit down and think about exactly what you are trying to display, and what data you want to display where - because without that you are just guessing what to do, and no one can really help you!
string s = ConfigurationManager.ConnectionStrings [constr]。ConnectionString;
string sel =select * from TableName ;
MySqlConnection con = new MySqlConnection(s);
MySqlCommand cmd = new MySqlCommand(sel,con);
MySqlDataAdapter sda = new MySqlDataAdapter (cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
gridview1.DataSource = ds;
gridview1.DataBind();
返回View();
string s = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sel = "select * from TableName";
MySqlConnection con = new MySqlConnection(s);
MySqlCommand cmd = new MySqlCommand(sel, con);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
gridview1.DataSource = ds;
gridview1.DataBind();
return View();
这篇关于如何使用数据表(数据集)填充Datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文