This is a migrated thread and some comments may be shown as answers.

Deletimg records in My Radgrid

1 Answer 27 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yong
Top achievements
Rank 2
Yong asked on 14 Oct 2008, 03:18 PM
Hi,

Need some advice on how to do the follwoing

Delete a row from my prodTable based on the datakeyvalues ProdType

Here is the Code

// Adding product

protected void AddProdBtn_Click(object sender, EventArgs e)
    {
        double qty = double.Parse(ProdUnitUserTxtBox.Text.ToString());
        double cost = double.Parse(ProdCostTxtBox.Text.ToString());
        double total = qty * cost;
        ProdTotalAmtLbl.Text = total.ToString();

        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("ProdSerialNo"));
        dt.Columns.Add(new DataColumn("ProdSerialRemark"));
        dt.Columns.Add(new DataColumn("ProdType"));
        dt.Columns.Add(new DataColumn("ProdUnitUser"));
        dt.Columns.Add(new DataColumn("ProdCost"));
        dt.Columns.Add(new DataColumn("ProdTotalAmt"));

        DataRow dr;

        double totalAmt = 0;

        GridDataItemCollection dtCol = ProdTable.Items;
        foreach (GridDataItem item in dtCol)
        {
            dr = dt.NewRow();
            dr["ProdSerialNo"] = item["ProdSerialNo"].Text;
            dr["ProdSerialRemark"] = item["ProdSerialRemark"].Text;
            dr["ProdType"] = item["ProdType"].Text;
            dr["ProdUnitUser"] = item["ProdUnitUser"].Text;
            dr["ProdCost"] = item["ProdCost"].Text;
            dr["ProdTotalAmt"] = item["ProdTotalAmt"].Text;
            totalAmt += double.Parse(item["ProdTotalAmt"].Text.ToString());

            dt.Rows.Add(dr);
        }

        dr = dt.NewRow();
        dr["ProdSerialNo"] = SerialNoTxtBox.Text;
        dr["ProdSerialRemark"] = SerialNoRemarksTxtBox.Text;
        dr["ProdType"] = ProdTypeComboBox.SelectedItem.Text.ToString();
        dr["ProdUnitUser"] = ProdUnitUserTxtBox.Text;
        dr["ProdCost"] = ProdCostTxtBox.Text;
        dr["ProdTotalAmt"] = ProdTotalAmtLbl.Text;
        totalAmt += double.Parse(ProdTotalAmtLbl.Text.ToString());

        dt.Rows.Add(dr);
        ProdTable.DataSource = dt.DefaultView;
        ProdTable.DataBind();

        ProdAllTotalLbl.Text = totalAmt.ToString();

        SerialNoTxtBox.Text = "";
        SerialNoRemarksTxtBox.Text = "";
        ProdUnitUserTxtBox.Text = "";
        ProdCostTxtBox.Text = "";
    }


// Delete The Product

    protected void ProdTable_DeleteCommand(object source, GridCommandEventArgs e)
    {
        string type = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProdType"].ToString();

        //string productType;

        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("ProdType"));

        

        GridDataItemCollection dtCol = ProdTable.Items;
        foreach (GridDataItem item in dtCol)
        {
            type = item["ProdType"].Text;
        }  

        if (dt.Rows.Find(type) != null)
        {
            dt.Rows.Find(type).Delete();
            dt.AcceptChanges();
        }
    }


Want to delete a row which has the unique data key values ProdType from the Radgrid prod Table

Need advice on how to do it corrrectly as i am still reading and learning.

Referring To The Following

    protected void ProdTable_DeleteCommand(object source, GridCommandEventArgs e)
    {
        string type = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProdType"].ToString();

        //string productType;

        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("ProdType"));

        

        GridDataItemCollection dtCol = ProdTable.Items;
        foreach (GridDataItem item in dtCol)
        {
            type = item["ProdType"].Text;
        }  

        if (dt.Rows.Find(type) != null)
        {
            dt.Rows.Find(type).Delete();
            dt.AcceptChanges();
        }
    }

private void RadGrid1_DeleteCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
  string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CustomerID"].ToString();
  DataTable table = (DataTable)Session["DataSource"];
  if(table.Rows.Find(ID) != null)
  {
    table.Rows.Find(ID).Delete();
    table.AcceptChanges();
    Session["DataSource"] = table;
  }
}


Thank You For Reading

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 16 Oct 2008, 12:59 PM
Hi Yong,

Basically, the delete operation has to do with manipulation of the underlying database directly, since in this case the event is not triggered by the grid itself. Therefore, you need to directly access the underlying datasource, delete all the required rows, and rebind the control.
I hope this suggestion helps.

Greetings,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Yong
Top achievements
Rank 2
Answers by
Yavor
Telerik team
Share this question
or