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

Normal grid to radgrid

1 Answer 38 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sizo
Top achievements
Rank 1
Sizo asked on 16 Jul 2012, 06:44 AM
Can anyone please help me convert my grid to a radgrid.

Finally got it to work by using the telerik documentation page...

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Jul 2012, 09:33 AM
Hi Sizo,

Here is the sample code that I tried which performs insert, update, delete operation in radgrid.
aspx:
<telerik:RadGrid ID="rdgdr1" runat="server" AutoGenerateColumns="false" AutoGenerateEditColumn="true" onneeddatasource="rdgdr1_NeedDataSource" ondeletecommand="rdgdr1_DeleteCommand"oninsertcommand="rdgdr1_InsertCommand"
            onupdatecommand="rdgdr1_UpdateCommand"
            onitemdatabound="rdgdr1_ItemDataBound" onitemcommand="rdgdr1_ItemCommand"
            onitemupdated="rdgdr1_ItemUpdated" onitemcreated="rdgdr1_ItemCreated">
 
<MasterTableView DataKeyNames="OrderID"  CommandItemDisplay="top">
  <Columns>
    <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" ></telerik:GridBoundColumn>
    <telerik:GridBoundColumn UniqueName="EmployeeID" DataField="EmployeeID" HeaderText="EmployeeID"></telerik:GridBoundColumn>
  </Columns>
 </MasterTableView>
</telerik:RadGrid>
C#:
public SqlCommand SqlCommand = new SqlCommand();
protected void rdgdr1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
  string selectFilterQuery = "select top 5 * from [Orders]";
  SqlCommand.CommandText = selectFilterQuery;
  SqlDataAdapter adapter = new SqlDataAdapter(selectFilterQuery, conn);
  DataTable dt = new DataTable();
  conn.Open();
  adapter.Fill(dt);
  conn.Close();
  rdgdr1.DataSource = dt;
}
protected void rdgdr1_UpdateCommand(object sender, GridCommandEventArgs e)
{
 conn.Open();
 GridEditableItem editItem = (GridEditableItem)e.Item;
 int index = editItem.ItemIndex;
 string orderId = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["OrderID"].ToString();
 string OrderId = (editItem["OrderID"].Controls[0] as TextBox).Text;
 string EmployeeID = (editItem["EmployeeID"].Controls[0] as TextBox).Text;
 string OrderDate = (editItem["OrderDate"].Controls[0] as TextBox).Text;
 string updateQuery = "Update Orders set ShipCity='"+city+"', EmployeeID='" + EmployeeID + "',OrderDate='" + OrderDate + "'" where OrderID='" + OrderId + "'";
 SqlCommand.CommandText = updateQuery;
 SqlCommand.Connection = conn;
 SqlCommand.ExecuteNonQuery();
 conn.Close();
}
protected void rdgdr1_DeleteCommand(object sender, GridCommandEventArgs e)
{
  conn.Open();
  GridDataItem editItem = (GridDataItem)e.Item;
  string orderID = editItem.OwnerTableView.DataKeyValues[editItem.ItemIndex]["OrderID"].ToString();
  string delQuery="delete from orders where OrderID='"+orderID+"'";
  SqlCommand.CommandText = delQuery;
  SqlCommand.Connection = conn;
  SqlCommand.ExecuteNonQuery();
  conn.Close();
}
protected void rdgdr1_InsertCommand(object sender, GridCommandEventArgs e)
{
  conn.Open();
  GridEditableItem editItem = (GridEditableItem)e.Item;
  string OrderId = (editItem["OrderID"].Controls[0] as TextBox).Text;
  string EmployeeID = (editItem["EmployeeID"].Controls[0] as TextBox).Text;
  string insertQuery = "insert into Orders (OrderID,EmployeeID) values ('" + OrderId + "','" + EmployeeID + "'");
  SqlCommand.CommandText = insertQuery;
  SqlCommand.Connection = conn;
  SqlCommand.ExecuteNonQuery();
  conn.Close();
}
Also check the following demo which explains declarative operations in radgrid.
Grid / Automatic Operations

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