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

Can RadGrid show temporary values?..

4 Answers 171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 20 Oct 2010, 09:23 AM
Hey everyone,

I have a grid,its data source is a data table.I've created columns in the data table.and at page_load grid is visible with no records in it & just names of the columns.Now,I've a button external to the grid which i am using to open up the insert Form.I've created form template for inserting values.It also have 2 buttons Save and Cancel.

Now,on Save button I put all the values inserted by the user in the first row of the data table.And it is visible in the data Table when i see using breakpoints.But My problem is that it remains in the Data Table and is not Visible in the Grid.I want Grid to show values inside the data Table and values keeps on increasing as user inserts more items to the Data Table.

My code for CS is--
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dtValues = new DataTable();
            dtValues.Columns.Add("Items");
            dtValues.Columns.Add("Rate");
            dtValues.Columns.Add("Quantity");
            dtValues.Columns.Add("Amount");
            RadGrid1.DataSource = dtValues;
        }
protected void btnAdd_Click(object sender, EventArgs e)
        {
            RadGrid1.MasterTableView.IsItemInserted = true;
            RadGrid1.MasterTableView.Rebind();
        }
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
                RadComboBox combo = insertItem.FindControl("RadComboBox1") as RadComboBox;
                combo.AutoPostBack = true;
                combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
                Button Save = insertItem.FindControl("btnSave") as Button;
                Save.Click += new EventHandler(Save_Click);
                Button cancel = insertItem.FindControl("btnCancel") as Button;
                cancel.Click += new EventHandler(Cancel_Click);
            }
        }
protected void Cancel_Click(object sender, EventArgs e)
        {
            Button Cancel = (Button)sender;
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)Cancel.NamingContainer;
            RadComboBox combo = insertItem.FindControl("RadComboBox1") as RadComboBox;
            Label lblRate = (Label)insertItem.FindControl("lblRate");
            RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem.FindControl("txtQuantityE");
            Label lblAmount = (Label)insertItem.FindControl("lblAmount");
            combo.SelectedIndex = -1;
            lblRate.Text = "";
            txtQauntityE.Text = "";
            lblAmount.Text = "";
            RadGrid1.MasterTableView.IsItemInserted = false;
            RadGrid1.MasterTableView.Rebind();
        }
 
        protected void Save_Click(object sender, EventArgs e)
        {
            Button Save = (Button)sender;
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)Save.NamingContainer;
            RadComboBox combo = insertItem.FindControl("RadComboBox1") as RadComboBox;
            Label lblRate = (Label)insertItem.FindControl("lblRate");
            RadNumericTextBox txtQauntityE = (RadNumericTextBox)insertItem.FindControl("txtQuantityE");
            Label lblAmount = (Label)insertItem.FindControl("lblAmount");
            if (combo.SelectedIndex > 0 && txtQauntityE.Text != null)
            {
                DataRow drValues = dtValues.NewRow();
                drValues["Items"] = combo.SelectedItem.Text;
                drValues["Rate"] = lblRate.Text;
                drValues["Quantity"] = txtQauntityE.Text;
                drValues["Amount"] = lblAmount.Text;
                dtValues.Rows.Add(drValues);
                dtValues.AcceptChanges();
                RadGrid1.DataSource = dtValues;
                RadGrid1.Rebind();
                RadGrid1.MasterTableView.IsItemInserted = false;
                RadGrid1.MasterTableView.Rebind();
            }
            else
            {
                 
            }
        }
protected void combo_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox combo = (RadComboBox)o;
            GridEditFormInsertItem insertItem = (GridEditFormInsertItem)combo.NamingContainer;
            Label lblRate = (Label)insertItem.FindControl("lblRate");
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn"].ToString());
            SqlCommand cmd = new SqlCommand("select [Rate] FROM [tblProducts] where ProductName=@ProductName", conn);
            cmd.Parameters.Add(new SqlParameter("@ProductName", combo.SelectedValue));
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblRate.Text = ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();
            }
            RadNumericTextBox Quantity = (RadNumericTextBox)insertItem.FindControl("txtQuantityE");
            Quantity.Focus();
        }
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
            }
        }
What's wrong with this,Am i missing something OR is this NOT POSSIBLE to insert temporary items to grid using data Table as Data Source.Plz help...

Thanks
Amit

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 20 Oct 2010, 11:34 AM
Hello Amit,

In order to avoid this issue store the DataTable in a session variable which  will retrieve all the previously stored record in DataTable. Check out the following code snippet.
 
C#:
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        dtValues = new DataTable();
        dtValues.Columns.Add("Items");
        dtValues.Columns.Add("Rate");
        dtValues.Columns.Add("Quantity");
        dtValues.Columns.Add("Amount");
        if (Session["Table"] != null)
        {
            dtValues = (DataTable)Session["Table"]; // retrieve DataTable from session
        }
        RadGrid1.DataSource = dtValues;
        Session["Table"] = dtValues; //store DataTable in session
    }
 protected void Save_Click(object sender, EventArgs e)
    {
       . . . . . . . . . . .
        if (combo.SelectedIndex > 0 txtQauntityE.Text != null)
        {
            dtValues = (DataTable)Session["Table"];// retrieve DataTable from session
            DataRow drValues = dtValues.NewRow();
            drValues["Items"] = combo.SelectedItem.Text;
            drValues["Rate"] = lblRate.Text;
            drValues["Quantity"] = txtQauntityE.Text;
            drValues["Amount"] = lblAmount.Text;
            dtValues.Rows.Add(drValues);
            dtValues.AcceptChanges();
            Session["Table"] = dtValues; //store DataTable in session
            RadGrid1.Rebind();
            RadGrid1.MasterTableView.IsItemInserted = false;
            RadGrid1.MasterTableView.Rebind();
        }
  }

Thanks,
Princy.
0
zel
Top achievements
Rank 1
answered on 09 Mar 2012, 09:38 AM
Hello Princy!

I know this post is old but i find it helpful. I am using a Session in storing the values from data grid before saving it to the database. But I am facing another problem on how to use the datatable session for "Edit" or "UpdateCommand"?

Any help will be really appreciated.

Thanks!
Zel
0
asimptota
Top achievements
Rank 1
answered on 13 Sep 2012, 11:34 AM
Hello Zel,
 I'm trying to accomplish storing RadGrid data into session before saving it to the database. Could you please post some code snippet on how to do that. It would be very helpful!
0
Kostadin
Telerik team
answered on 18 Sep 2012, 09:06 AM
Hi,

You could take a look at this demo where is shown how to store updated values in a DataSet living in Session. Note that doing this with DataTable is identical.

Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
zel
Top achievements
Rank 1
asimptota
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or