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

How to create a blank Radgrid?

4 Answers 234 Views
Grid
This is a migrated thread and some comments may be shown as answers.
shemein
Top achievements
Rank 1
shemein asked on 30 Mar 2014, 08:55 PM
I need to create a blank RadGrid  so that a user can add or remove their information in a sql server table. I don't want to bind the RadGrid to the table to display the content of the table inside the Radgird but also I want to use RadGrid to manipulate the data in sql server like insert or delete the information. Would you please help me how to do this?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 Mar 2014, 05:56 AM
Hi Zahra,

I guess you want to bind the RadGrid to bind to a empty datasource, please try as follows:

C#:
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
      RadGrid1.DataSource = String.Empty;
  }
}
 
If you always want grid to be empty you can use the NeedDataSource event of the RadGrid, and handle the Insert/Delete operation in the OnInsertCommand/OnDeleteCommand event of the radgrid.

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = string.Empty;
}

Please try and let me know if any concern.
Thanks,
Princy
0
shemein
Top achievements
Rank 1
answered on 31 Mar 2014, 03:39 PM
Hi Princy,

Thanks a lot for your reply but my problem is I have a table that when a client click on add new link  in the empty RadGrid and input the information  those information can be inserted in the table or deleted from the table. Do you have any idea?

Regards,
Zahra
0
shemein
Top achievements
Rank 1
answered on 31 Mar 2014, 04:24 PM
Princy,

If I ask this it is because I have a business object layer that I don't know how I should bound my insertcommand and deletecommand into it.
0
Princy
Top achievements
Rank 2
answered on 02 Apr 2014, 08:28 AM
Hi Zahra,

I guess you want to hide the RadGrid data but perform the Insert/Delete operation. You can bind the grid to empty datasource as shown above using RadGrid1.DataSource = String.Empty in the OnNeedDataSource event. You can handle the insert operation in the OnInsertCommand event of the radgrid. But I'm not sure if you have no data in the table how can you delete the row? The insert can be handled as below.

C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
  GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
  //Accessing boundcolumn values in edit mode 
  string ID = (insertedItem["ID"].Controls[0] as TextBox).Text;
  string Name = (insertedItem["Name"].Controls[0] as TextBox).Text;     
  try
  {
   //Code to insert to db
  }
  catch (Exception ex)
  {
    RadGrid1.Controls.Add(new LiteralControl("Unable to insert . Reason: " + ex.Message));
    e.Canceled = true;
  }
}

Thanks,
Princy
Tags
Grid
Asked by
shemein
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
shemein
Top achievements
Rank 1
Share this question
or