我想每次从数据集ds到dc添加一行。我想继续更新Gridview以扩大表格。但是当我在我的代码中尝试它时。 Gridview只能根据我在文本框中输入的内容显示一行。
public partial class TestGridview : System.Web.UI.Page
{
int count = 0;
DataSet dc = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
string text = TextBox1.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["XMLConnectionString"].ConnectionString);
// Create the command object
string str = "SELECT * FROM XML WHERE [Part_Numbber] = @textInput";
SqlCommand cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("textInput", text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "XML");
if (count == 0)
{
dc = ds.Clone();
count ++;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i].ItemArray[0].ToString() != "NULL")
{
dc.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
}
}
GridView1.DataSource = dc;
GridView1.DataBind();
}
}
答案 0 :(得分:0)
您可以使用以下代码将1 DT中的行追加到其他最终DT。 LikeWise
foreach (DataRow dataRow in dt2.Rows)
{
DataRow dr = dt1.NewRow();
dr[0] = Convert.ToInt32(dataRow[0]);
dr[1] = dataRow[1].ToString();
dr[2] = dataRow[2].ToString();
dt1.Rows.Add(dr);
}
foreach (DataRow dataRow in dt3.Rows)
{
DataRow dr = dt1.NewRow();
dr[0] = Convert.ToInt32(dataRow[0]);
dr[1] = dataRow[1].ToString();
dr[2] = dataRow[2].ToString();
dt1.Rows.Add(dr);
}
foreach (DataRow dataRow in dt4.Rows)
{
DataRow dr = dt1.NewRow();
dr[0] = Convert.ToInt32(dataRow[0]);
dr[1] = dataRow[1].ToString();
dr[2] = dataRow[2].ToString();
dt1.Rows.Add(dr);
}