5

テキストボックスからデータを取得したいのですが、ボタンをクリックすると、そのデータが に挿入されGridViewます。クリックするたびに新しい行が作成され、古い行は削除されません。新しいデータを入力してボタンをクリックすると、古い行が削除され、代わりに新しい行が保存されます。これが私のコードです:

DataTable dt1 = new DataTable();
bool flag = false;  

private void gridVIEWData()
{
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
}
protected void Button3_Click(object sender, EventArgs e)
{
    if (!flag)
    {
        gridVIEWData();
        flag = true;
        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
    else if (!IsPostBack)
    {
        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}
4

2 に答える 2

2

新しいデータとバインドすると、古いデータは削除されます。あなたの場合、データテーブルのデータソースに古いデータを保持する必要があります。通常、ファイルのデータベースのようなデータを保存するための永続的なメディアがあります。

あなたの理解のために、データテーブルをセッションに保存しますが、通常は実践されていません。データベースまたは好きなメディアに保存する必要があります。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;
        GridView1.DataBind();
    }
}

private void gridVIEWData() {
   dt1.Columns.Add("pName", typeof(string));
   dt1.Columns.Add("pCategory", typeof(string));
   dt1.Columns.Add("price", typeof(string));
   dt1.Columns.Add("pQuantity", typeof(string));
   dt1.Columns.Add("totalPrice", typeof(string));
   Session["dtInSession"] = dt1;     //Saving Datatable To Session 
}


protected void Button3_Click(object sender, EventArgs e)
{
        if(Session["dtInSession"] != null)
             dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 

        Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
        DataRow dr = dt1.NewRow();
        dr["pName"] = DropDownList2.SelectedItem;
        dr["pCategory"] = DropDownList1.SelectedItem;
        dr["price"] = txt_price.Text;
        dr["pQuantity"] = txt_quantity.Text;
        dr["totalPrice"] = total;
        dt1.Rows.Add(dr);

        Session["dtInSession"] = dt1;     //Saving Datatable To Session 
        GridView1.DataSource = dt1;
        GridView1.DataBind();

    }
 }
于 2012-10-13T18:44:40.700 に答える
1

これは、gridVIEWData();テーブル列を再度作成するポストバックで毎回呼び出しているためです。

これを試して

DataTable dt1 = new DataTable();

private void gridVIEWData() {
    dt1.Columns.Add("pName", typeof(string));
    dt1.Columns.Add("pCategory", typeof(string));
    dt1.Columns.Add("price", typeof(string));
    dt1.Columns.Add("pQuantity", typeof(string));
    dt1.Columns.Add("totalPrice", typeof(string));
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridVIEWData();
        GridView1.DataSource = dt1;
        GridView1.DataBind();
        Session["dt1"]=dt1;
    }
}

protected void Button3_Click(object sender, EventArgs e)
{
    if(Session["dt1"]!=null) dt1 = (DataTable) Session["dt1"];
    Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text);
    DataRow dr = dt1.NewRow();
    dr["pName"] = DropDownList2.SelectedItem.Text;
    dr["pCategory"] = DropDownList1.SelectedItem.Text;
    dr["price"] = txt_price.Text;
    dr["pQuantity"] = txt_quantity.Text;
    dr["totalPrice"] = total.ToString();
    dt1.Rows.Add(dr);

    GridView1.DataSource = dt1;
    GridView1.DataBind();
}
于 2012-10-13T18:23:20.500 に答える