![](/img/trans.png)
[英]How to display data from Database after selecting a row from Gridview
[英]How to display single row as multiple row from database to gridview?
如何在数据库视图的gridview中将具有多个值的单行显示为多行?
例:
具有值的数据库行-也是我的gridview结果
两个订单的ID为:1
结果需要是:
两个订单的ID为:1
我到目前为止有什么
DataTable dt = new DataTable();
string cs = ConfigurationManager.ConnectionStrings["mk"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("SELECT bookName, numOfItems, category, FROM [Orders]))
{
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
try
{
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
gw1.DataSource = dt;
gw1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
更新
DataTable dt = new DataTable();
string cs = ConfigurationManager.ConnectionStrings["mk"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("SELECT bookName, numOfItems, category FROM [Order]"))
{
var newdt= dt.Clone();
foreach (DataRow row in dt.Rows)
{
var bookNames= row.Field<string>("bookName").Split(',');
var numOfItems= row.Field<string>("numOfItems").Split(',');
var categorys= row.Field<string>("category").Split(',');
//assume all columns having asme number of items seperated by comma
for (int i = 0; i < bookNames.Length; i++)
{
var newRow = newdt.Rows.Add();
newRow.SetField("bookName", bookNames[i]);
newRow.SetField("numOfItems", numOfItems[i]);
newRow.SetField("category", categorys[i]);
}
}
gw1.DataSource = newdt;
gw1.DataBind();
}
}
DataTable dt = new DataTable();
string cs = ConfigurationManager.ConnectionStrings["mk"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("SELECT bookName, numOfItems, category FROM [Order]"))
{
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
var newdt= dt.Clone();
foreach (DataRow row in dt.Rows)
{
var bookNames= row.Field<string>("bookName").Split(',');
var numOfItems= row.Field<string>("numOfItems").Split(',');
var categorys= row.Field<string>("category").Split(',');
//assume all columns having asme number of items seperated by comma
for (int i = 0; i < bookNames.Length; i++)
{
var newRow = newdt.Rows.Add();
newRow.SetField("bookName", bookNames[i]);
newRow.SetField("numOfItems", numOfItems[i]);
newRow.SetField("category", categorys[i]);
}
}
gw1.DataSource = newdt;
gw1.DataBind();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.