如何在DataTable中的DetailsView中动态存储一个数据? [英] How to store one by one data dynamically in DetailsView from DataTable?

查看:120
本文介绍了如何在DataTable中的DetailsView中动态存储一个数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在DataView中的DetailsView中动态存储一个数据,并在DetailsView中显示结果。

将数据存储到数据库表后再显示。 br />
假设,Product表的Table属性是

1. ID(Int)(自动增量)

2.特征(Varchar2(50) ))



我想将产品名称,颜色和价格存储到一列中,即功能;

Hi,
I want to store one by one data dynamically in DetailsView from DataTable and display result in DetailsView.
After Store data into database table then display.
Suppose, Table attribute of "Product" table are
1. ID (Int) (Auto Increment)
2. Features (Varchar2(50))

I want to store "Product name", "Color" and "Price" into only one column i.e, "Features";

protected void Button1_Click(object sender, EventArgs e)
{
string item1 = TextBox1.Text + "\n"; // Product Name
string item2 = TextBox2.Text + "\n"; // Color
string item3 = TextBox3.Text + "\n"; // Price
string AllItem = item1 + item2 + item3; // Suppose: AllItem= Nokia 5230\nBlack\n7500
con.Open();
cmd.Connection = con;
cmd.CommandText = "insert into product values('" + AllItem + "')";
cmd.ExecuteNonQuery();
con.Close();
}



然后我尝试将值单独存储到DataTable然后显示DetailsView:


Then I try to store value separately into DataTable Then display DetailsView:

protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
con.Open();
cmd.Connection = con;
cmd.CommandText = "Select Features from Product where id=5";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string result = dr["Features"].ToString();
int ln = result.Length;
dt.Columns.Add("items");
int index = result.IndexOf("\n");
while (ln > 0)
{
string a = result.Substring(0, index);
dt.Rows.Add(a);
ln = ln - (index + 1);
result = result.Substring(index + 1, ln);
index = result.IndexOf("\n");
}
}
int len = dt.Rows.Count;
for (int i = 0; i < len; i++)
{
DetailsView1.Rows.Cells.Text = dt.Rows[1].ToString();
}
DetailsView1.DataBind();
}

}



在DataTable dt中存储值。那是


Store value in DataTable dt. That is

dt.Rows[0][0]=Nokia 5230
dt.Rows[1][0]-Black
dt.Rows[2][0]=7500





我想要将这些值存储在DetailsView的以下结构中

产品名称:诺基亚5230

颜色:黑色

价格:7500



但不能存储。

请解决我的问题。



I want to store these value in following structure in DetailsView
Product Name: Nokia 5230
Color: Black
Price: 7500

But can't be store.
Please solve my problem.

推荐答案

最好的解决方案是创建一个asp.net Repeater并将项目绑定到它。
Best solution for this would be to create a asp.net Repeater and bind the item to it.


这篇关于如何在DataTable中的DetailsView中动态存储一个数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆