Here is the following Code
<radG:RadGrid ID="RadGrid1" Skin="Lime" runat="server" CssClass="RadGrid" GridLines="None" AllowPaging="True" PageSize="5" AllowSorting="True" Width="99%" AutoGenerateColumns="False" EnableAJAX="True" ShowStatusBar="true" HorizontalAlign="NotSet" Source="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand" OnInsertCommand="RadGrid1_InsertCommand"
OnDeleteCommand="RadGrid1_DeleteCommand">
<PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
<MasterTableView CommandItemDisplay="Top" GridLines="None" DataKeyNames="CustomerID"
EditMode="InPlace">
<Columns>
<radG:GridEditCommandColumn>
</radG:GridEditCommandColumn>
<radG:GridBoundColumn UniqueName="CustomerID" HeaderText="ID" ReadOnly="true" DataField="CustomerID">
</radG:GridBoundColumn>
<radG:GridBoundColumn UniqueName="CompanyName" HeaderText="CompanyName" DataField="CompanyName">
</radG:GridBoundColumn>
<radG:GridBoundColumn UniqueName="ContactName" HeaderText="ContactName" DataField="ContactName">
</radG:GridBoundColumn>
<radG:GridBoundColumn UniqueName="Address" HeaderText="Address" DataField="Address">
</radG:GridBoundColumn>
<radG:GridButtonColumn CommandName="Delete" Text="Delete">
</radG:GridButtonColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<EditColumn UniqueName="EditCommandColumn1">
</EditColumn>
<FormTemplate>
<table id="Table2" cellspacing="2" cellpadding="1" width="250" border="1" rules="none"
style="border-collapse: collapse">
<tr>
<td>
<table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0">
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
CustomerID:
</td>
<td>
<asp:TextBox ID="txtCustomerID" MaxLength="5" Visible='<% # (Container as GridItem).OwnerTableView.IsItemInserted %>'
runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
CompanyName:
</td>
<td>
<asp:TextBox ID="txtCompanyName" runat="server" Text='<%# Eval( "CompanyName" ) %>'>
</asp:TextBox>
</td>
</tr>
<tr>
<td>
ContactName:
</td>
<td>
<asp:TextBox ID="txtContactName" runat="server" Text='<%# Eval( "ContactName") %>'
TabIndex="1">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval( "Address") %>' TabIndex="2">
</asp:TextBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<b>Company Info:</b>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="btnUpdate" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'>
</asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</table>
</FormTemplate>
</EditFormSettings>
<ExpandCollapseColumn Visible="False">
<HeaderStyle Width="19px" />
</ExpandCollapseColumn>
<RowIndicatorColumn Visible="False">
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
</MasterTableView>
</radG:RadGrid>
Code Behined:-
//Declare a global DataTable dtTable
public static DataTable dtTable;
//Declare a global SqlConnection SqlConnection
//public SqlConnection SqlConnection = new SqlConnection("Data Source=local;Initial Catalog=Northwind;User ID=**");
public SqlConnection SqlConnection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;uid=sa;pwd=password;");
//Declare a global SqlDataAdapter SqlDataAdapter
public SqlDataAdapter SqlDataAdapter = new SqlDataAdapter();
//Declare a global SqlCommand SqlCommand
public SqlCommand SqlCommand = new SqlCommand();
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
//Populate the Radgrid
dtTable = new DataTable();
//Open the SqlConnection
SqlConnection.Open();
try
{
//Select Query to populate the RadGrid with data from table Customers.
string selectQuery = "SELECT CustomerID,CompanyName,ContactName,Address FROM Customers";
SqlDataAdapter.SelectCommand = new SqlCommand(selectQuery, SqlConnection);
SqlDataAdapter.Fill(dtTable);
RadGrid1.DataSource = dtTable;
}
finally
{
//Close the SqlConnection
SqlConnection.Close();
}
}
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
//Get the GridEditableItem of the RadGrid
GridEditableItem editedItem = e.Item as GridEditableItem;
//Get the primary key value using the DataKeyValue.
string CustomerID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["CustomerID"].ToString();
//Access the textbox from the edit form template and store the values in string variables.
string CompanyName= (editedItem.FindControl("txtCompanyName") as TextBox).Text;
string ContactName = (editedItem.FindControl("txtContactName") as TextBox).Text;
string Address = (editedItem.FindControl("txtAddress") as TextBox).Text;
try
{
//Open the SqlConnection
SqlConnection.Open();
//Update Query to update the Datatable
string updateQuery = "UPDATE Customers set CompanyName='"+CompanyName+"',ContactName='"+ContactName+"',Address='"+Address+"' where CustomerID='" + CustomerID+"'";
SqlCommand.CommandText = updateQuery;
SqlCommand.Connection = SqlConnection;
SqlCommand.ExecuteNonQuery();
//Close the SqlConnection
SqlConnection.Close();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to update Customers. Reason: " + ex.Message));
e.Canceled = true;
}
}
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
//Get the GridEditFormInsertItem of the RadGrid
GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item;
//GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
//Access the textbox from the insert form template and store the values in string variables.
string CustomerID = (insertedItem.FindControl("txtCustomerID") as TextBox).Text;
string CompanyName = (insertedItem.FindControl("txtCompanyName") as TextBox).Text;
string ContactName = (insertedItem.FindControl("txtContactName") as TextBox).Text;
string Address = (insertedItem.FindControl("txtAddress") as TextBox).Text;
try
{
//Open the SqlConnection
SqlConnection.Open();
//Update Query to insert into the database
string insertQuery = "INSERT into Customers(CustomerID,CompanyName,ContactName,Address) values('"+CustomerID+"','"+ CompanyName + "','"+ContactName+"','"+Address+"')";
SqlCommand.CommandText = insertQuery;
SqlCommand.Connection = SqlConnection;
SqlCommand.ExecuteNonQuery();
//Close the SqlConnection
SqlConnection.Close();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to insert Customers. Reason: " + ex.Message));
e.Canceled = true;
}
}
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
{
//Get the GridDataItem of the RadGrid
GridDataItem item = (GridDataItem)e.Item;
//Get the primary key value using the DataKeyValue.
string CustomerID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["CustomerID"].ToString();
try
{
//Open the SqlConnection
SqlConnection.Open();
string deleteQuery = "DELETE from Customers where CustomerID='" + CustomerID + "'";
SqlCommand.CommandText = deleteQuery;
SqlCommand.Connection = SqlConnection;
SqlCommand.ExecuteNonQuery();
//Close the SqlConnection
SqlConnection.Close();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(new LiteralControl("Unable to delete Customers. Reason: " + ex.Message));
e.Canceled = true;
}
}
But I am not able to either Insert/Update Records.
Please Help me in this regards.
I want to Insert/Update Records only in In-Place Mode.
At the same time I want to Bind the Combo Box(Which is Grid) from Other table recors.
Please Guide me…..
| <telerik:GridTemplateColumn UniqueName="TemplateCol" HeaderText="TemplateCol"> |
| <ItemTemplate> |
| <asp:Label id="lblName" runat="server" Text='<%#Eval("ProductName") %>' ></asp:Label> |
| <asp:Panel ID="popupMenu" runat="server" style="display:none" BackColor="skyblue" > |
| <asp:Button ID="btnCopy" runat="server"></asp:Button> |
| </asp:Panel> |
| <cc1:HoverMenuExtender ID="HoverMenuExtender1" TargetControlID="lblName" runat="server" |
| PopupControlID="popupMenu" PopupPosition="right" PopDelay="50"> |
| </cc1:HoverMenuExtender> |
| </ItemTemplate> |
| </telerik:GridTemplateColumn> |
private
void AddTab(string tabName)
{
RadTab tab = new RadTab();
tab.Text = tabName;
RadTabStrip.SelectedIndex = selectedTabIndex;
RadTabStrip.Tabs.Add(tab);
RadPageView pageView = new RadPageView();
pageView.ID = tabName;
RadMultiPage.SelectedIndex = selectedTabIndex;
RadMultiPage.PageViews.Add(pageView);
}
protected
void RadMultiPage_PageViewCreated(object sender, RadMultiPageEventArgs e)
{
string userControlName = "UserControls/VMDetails.ascx";
Control userControl = LoadControl(userControlName, passName, passIP);
userControl.ID = passName +
"_vmdtl";
e.PageView.Controls.Add(userControl);
}
| protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
| { |
| GridEditableItem editedItem = e.Item as GridEditableItem; |
| UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID); |
| switch (e.CommandName) |
| { |
| case "PerformInsert": |