0

オンライン注文の基本情報(部品、数量、出荷先、要求日、出荷方法)を表示するグリッドビューがあります。グリッドビューで「編集」または「削除」リンクを選択すると、何らかの理由でどちらも実行できなくなります。これが私が持っているものです:

protected void Page_Load(object sender, EventArgs e)
    {                          
        table = new DataTable("myTable");
        DataColumn column1 = new DataColumn();
        column1.DataType = typeof(string);
        column1.ColumnName = "Part";
        DataColumn column2 = new DataColumn();
        column2.DataType = typeof(Int32);
        column2.ColumnName = "Quantity";
        DataColumn column3 = new DataColumn();
        column3.DataType = typeof(string);
        column3.ColumnName = "Ship-To";
        DataColumn column4 = new DataColumn();
        column4.DataType = typeof(string);
        column4.ColumnName = "Requested Date";
        DataColumn column5 = new DataColumn();
        column5.DataType = typeof(string);
        column5.ColumnName = "Shipping Method";
        table.Columns.Add(column1);
        table.Columns.Add(column2);
        table.Columns.Add(column3);
        table.Columns.Add(column4);
        table.Columns.Add(column5);
     }

    protected void addbtn_Click(object sender, EventArgs e)
    {
        //assign all variables in form
        part = parttxtbox.Text.ToUpper();
        shipto = shiptotxtbox.Text;
        reqdate = reqdatecal.SelectedDate.ToShortDateString();
        shipmthd = shipddl.SelectedItem.ToString();
        //validate to make sure all fields are in correct format and acceptable for processing
        if ((importlstbx.Items.Contains(item)) && (qtychk == 1) && (shipto.ToString() != "") &&
            (Convert.ToDateTime(reqdate) >= DateTime.Today) && (shipmthd != "-- Select --"))
        {
            validatelbl.Visible = false;
            Session["Part"] += part + "|";
            Session["Quantity"] += qty + "|";
            Session["Ship-To"] += shipto + "|";
            Session["Requested Date"] += reqdate + "|";
            Session["Shipping Method"] += shipddl.SelectedItem.ToString() + "|";
            CreateTable();

            //Clear All Data on Postback
            ClearTextBoxes(Page);
            //ClearDDLBoxes(Page);  
            SetFocus(parttxtbox);
        }
        orderbtn.Visible = true;
    }

    public void CreateTable()
    {
        string[] spart = Session["Part"].ToString().Split('|');
        string[] sqty = Session["Quantity"].ToString().Split('|');
        string[] sship = Session["Ship-To"].ToString().Split('|');
        string[] sdate = Session["Requested Date"].ToString().Split('|');
        string[] smethod = Session["Shipping Method"].ToString().Split('|');
        int recordnum = spart.Length;

        for (int i = 0; i < recordnum - 1; i++)
        {
            DataRow row = table.NewRow();
            row["Part"] = spart[i].ToString();
            row["Quantity"] = sqty[i].ToString();
            row["Ship-To"] = sship[i].ToString();
            row["Requested Date"] = sdate[i].ToString();
            row["Shipping Method"] = smethod[i].ToString();
            table.Rows.Add(row);
        }
        griditems.DataSource = table.DefaultView;
        griditems.DataBind();
    }

    private void BindData()
    {
        griditems.DataSource = table;
        griditems.DataBind();
    }

    protected void griditems_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        griditems.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected void griditems_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index. 
        griditems.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control. 
        BindData();
    }

    protected void griditems_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        griditems.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    }

    protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = table;

        //Update the values.
        GridViewRow row = griditems.Rows[e.RowIndex];
        dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Ship-To"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Requested Date"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["Shipping Method"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem;

        //Reset the edit index.
        griditems.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    }

    protected void griditems_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //int id = Convert.ToInt32(griditems.DataKeys[e.RowIndex].Values[0].ToString());
        //griditems.DeleteRow(id);
        //BindData();


        //string item = griditems.DataKeys[e.RowIndex].Value.ToString();
        //griditems.DeleteRow(Convert.ToInt32(item));
        //BindData();

        //TextBox FileContentsTextBox = parttxtbox.FindControl("part") as TextBox;
        //FileContentsTextBox.Text = string.Format("You have opted to delete {0}.", item);
        //File.Delete(item);

        //////((DataTable)ViewState["Data"]).Rows.RemoveAt(e.RowIndex);
        //((DataTable)table).Rows.RemoveAt(e.RowIndex);
        //BindData();
        ////griditems.DataSource = ((DataTable)table);
        ////griditems.DataBind();

    }

}

私は多くのことを試みました、それで多くがコメントアウトされる理由です。言うまでもなく、それは機能しませんでした。

4

3 に答える 3

0

ViewStateをグリッドに追加し、一度だけバインドする必要があります(投稿するときはテーブルを再作成しません)

If(! IsPostBack)
{
     //Create table

     table = new DataTable("myTable");
        DataColumn column1 = new DataColumn();
        column1.DataType = typeof(string);
        column1.ColumnName = "Part";
        DataColumn column2 = new DataColumn();
        column2.DataType = typeof(Int32);
        column2.ColumnName = "Quantity";
        DataColumn column3 = new DataColumn();
        column3.DataType = typeof(string);
        column3.ColumnName = "Ship-To";
        DataColumn column4 = new DataColumn();
        column4.DataType = typeof(string);
        column4.ColumnName = "Requested Date";
        DataColumn column5 = new DataColumn();
        column5.DataType = typeof(string);
        column5.ColumnName = "Shipping Method";
        table.Columns.Add(column1);
        table.Columns.Add(column2);
        table.Columns.Add(column3);
        table.Columns.Add(column4);
        table.Columns.Add(column5);

}

2ViewStateをgridViewに追加します

于 2012-08-20T17:29:39.870 に答える
0
protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
       {
        //Bind gridview
       }
     }

http://satindersinght.blogspot.in/2012/08/how-to-addupdate-record-using-gridview.html

于 2012-08-23T08:08:49.520 に答える
0

私はついに私の問題に対する答えを見つけました。これは、DataTableにバインドされたときに選択した行を削除できるようにするコードです。

protected void griditems_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = table;
        if (dt.Rows.Count > 0)
        {
            dt.Rows.RemoveAt(e.RowIndex);
            griditems.DataSource = dt;
            BindData();
        } 
    }
于 2012-08-24T16:51:06.057 に答える