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

i cannot update my data using datagrid(Using column editors)

1 Answer 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DragonBall
Top achievements
Rank 1
DragonBall asked on 06 Aug 2008, 10:33 AM

I have a problem updating my data what is wrong with my code? i followed the sample Extracting Values (using column Editors)

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Exam.ascx.cs" Inherits="Exam_Exam" %>  
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>  
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server" OnPreRender="Page_PreRender">  
</telerik:RadScriptManager>  
<asp:Label ID="Label1" Font-Bold="true" Font-Italic="true" Text='<%# Eval("semester_id") %>' Visible="true" runat="server" />  
<asp:SqlDataSource ID="SqlDataSource2" runat="server"   
    ConnectionString="<%$ ConnectionStrings:MeetWebStyleConnectionString %>"   
    SelectCommand="MWS_Exams_GetBySemester_id"   
    SelectCommandType="StoredProcedure" InsertCommand="MWS_Exams_Insert"   
    InsertCommandType="StoredProcedure" UpdateCommand="MWS_Exams_Update"   
    UpdateCommandType="StoredProcedure" DeleteCommand="MWS_Exams_Delete"   
    DeleteCommandType="StoredProcedure">  
    <SelectParameters>  
        <asp:ControlParameter ControlID="RadGrid1" DefaultValue="1" Name="Semester_id"   
            PropertyName="SelectedValue" Type="Int32" />  
    </SelectParameters>  
    <DeleteParameters>  
       <asp:ControlParameter ControlID="Label1" PropertyName="Text" Type="String" Name="semester_id" />  
    </DeleteParameters>  
    <UpdateParameters>  
    <asp:ControlParameter ControlID="Label1" PropertyName="Text" Type="String" Name="semester_id" />  
    </UpdateParameters>  
    <InsertParameters>  
        <asp:Parameter Direction="InputOutput" Name="Exam_id" Type="Int32" />  
        <asp:Parameter Name="Semester_id" Type="Int32" />  
        <asp:Parameter Name="Exam_name" Type="String" />  
        <asp:Parameter Name="Exam_description" Type="String" />  
        <asp:Parameter Name="Schedule_item_id" Type="Int32" />  
        <asp:Parameter Name="Library_id" Type="Int32" />  
        <asp:Parameter Name="Exam_result_grade_id" Type="Int32" />  
        <asp:Parameter Name="Start_date_range" Type="DateTime" />  
        <asp:Parameter Name="End_date_range" Type="DateTime" />  
        <asp:Parameter Name="Subject_id" Type="Int32" />  
        <asp:Parameter Name="Duration" Type="Decimal" />  
    </InsertParameters>  
</asp:SqlDataSource>  
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource2"   
    GridLines="None"   
    AutoGenerateEditColumn="false" AllowAutomaticDeletes="True"  AllowAutomaticUpdates="false"  
    AllowAutomaticInserts="True" AutoGenerateDeleteColumn="True" OnUpdateCommand="RadGrid1_UpdateCommand" OnNeedDataSource="RadGrid1_NeedDataSource"  >  
<MasterTableView DataSourceID="SqlDataSource2" AutoGenerateColumns="false"   
        CommandItemDisplay="Top" DataKeyNames="semester_id">  
 
<RowIndicatorColumn Visible="False">  
<HeaderStyle Width="20px"></HeaderStyle>  
</RowIndicatorColumn>  
 
<ExpandCollapseColumn Visible="False" Resizable="False">  
<HeaderStyle Width="20px"></HeaderStyle>  
</ExpandCollapseColumn>  
 
    <Columns>  
        <telerik:GridBoundColumn DataField="semester_id" HeaderText="Semester"   
            UniqueName="semester_id">  
        </telerik:GridBoundColumn>  
       <%-- <telerik:GridBoundColumn DataField="subjectName" HeaderText="Subject Name"   
            UniqueName="column2" >  
        </telerik:GridBoundColumn>  
          <telerik:GridBoundColumn DataField="examResult" HeaderText="Result"   
            UniqueName="column10">  
        </telerik:GridBoundColumn>--%>  
        <telerik:GridBoundColumn DataField="start_date_range" HeaderText="Start Date"   
            UniqueName="start_date_range">  
        </telerik:GridBoundColumn>  
        <telerik:GridBoundColumn DataField="end_date_range" HeaderText="End Date"   
            UniqueName="end_date_range">  
        </telerik:GridBoundColumn>  
        <telerik:GridBoundColumn DataField="exam_name" HeaderText="Exam Name"   
            UniqueName="exam_name">  
        </telerik:GridBoundColumn>  
         <telerik:GridEditCommandColumn UpdateText="Update" UniqueName="EditCommandColumn" CancelText="Cancel"  
                        EditText="Edit">  
         </telerik:GridEditCommandColumn>  
       <%-- <telerik:GridBoundColumn DataField="exam_description" HeaderText="Description"   
            UniqueName="column6">  
        </telerik:GridBoundColumn>  
         <telerik:GridBoundColumn DataField="schedule_item_id" HeaderText="Schedule"   
            UniqueName="column7">  
        </telerik:GridBoundColumn>  
         <telerik:GridBoundColumn DataField="library_id" HeaderText="Library"   
            UniqueName="column8">  
        </telerik:GridBoundColumn>  
         <telerik:GridBoundColumn DataField="Exam_result_grade_id" HeaderText="Result"   
            UniqueName="column9">  
        </telerik:GridBoundColumn>  
          <telerik:GridBoundColumn DataField="subject_id" HeaderText="subject"   
            UniqueName="column10">  
        </telerik:GridBoundColumn>  
          
         <telerik:GridBoundColumn DataField="duration" HeaderText="Duration"   
            UniqueName="column10">  
        </telerik:GridBoundColumn>--%>  
    </Columns>  
 
<EditFormSettings>  
<PopUpSettings ScrollBars="None"></PopUpSettings>  
</EditFormSettings>  
</MasterTableView>  
<ClientSettings Selecting-AllowRowSelect="true">  
<Selecting AllowRowSelect="True"></Selecting>  
    </ClientSettings>  
</telerik:RadGrid>  
 
 
 
 

using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Xml.Linq;  
using Telerik.Web.UI;  
using System.Data.SqlClient;  
public partial class Exam_Exam : System.Web.UI.UserControl  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
         
 
    }  
    protected void Page_PreRender(object sender, System.EventArgs e)  
    {  
         
        this.RadGrid1.MasterTableView.Rebind();  
        this.RadGrid1.Rebind();  
    }  
 
    public DataSet ExamData  
    {  
        get  
        {  
            object obj = this.Session["ExamsData"];  
            if (obj != null)  
            {  
                return (DataSet)obj;  
            }  
 
            DataSet examData = new DataSet();  
 
            String ConnString = ConfigurationManager.ConnectionStrings["MeetWebStyleConnectionString"].ConnectionString;  
            SqlConnection conn = new SqlConnection(ConnString);  
            SqlDataAdapter adapter = new SqlDataAdapter();  
            adapter.SelectCommand = new SqlCommand("SELECT semester_id,start_date_range,end_date_range,exam_name from MWS_Exams", conn);  
 
            adapter.Fill(examData, "Exam");  
 
            this.Session["ExamsData"] = examData;  
            return examData;  
        }  
    }  
 
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
    {  
          
        try  
        {  
 
            thisthis.RadGrid1.DataSource = this.ExamData;  
            this.ExamData.Tables["Exam"].PrimaryKey = new DataColumn[] { this.ExamData.Tables["Exam"].Columns["semester_id"] };  
 
        }  
        catch (Exception ex)  
        {  
            RadGrid1.Controls.Add(new LiteralControl("<strong>Unable to set value of column '" +  ex.Message));  
            
        }  
 
    }  
    protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
        GridEditableItem eeditedItem = e.Item as GridEditableItem;  
        GridEditManager editMan = editedItem.EditManager;  
 
        foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)  
        {  
            if (column is IGridEditableColumn)  
            {  
                IGridEditableColumn editableCol = (column as IGridEditableColumn);  
                if (editableCol.IsEditable)  
                {  
                    IGridColumnEditor editor = editMan.GetColumnEditor(editableCol);  
 
                    string editoreditorType = editor.ToString();  
                    string editorText = "unknown";  
                    object editorValue = null;  
 
                    if (editor is GridTextColumnEditor)  
                    {  
                        editorText = (editor as GridTextColumnEditor).Text;  
                        editorValue = (editor as GridTextColumnEditor).Text;  
                    }  
 
                    if (editor is GridBoolColumnEditor)  
                    {  
                        editorText = (editor as GridBoolColumnEditor).Value.ToString();  
                        editorValue = (editor as GridBoolColumnEditor).Value;  
                    }  
 
                    if (editor is GridDropDownColumnEditor)  
                    {  
                        editorText = (editor as GridDropDownColumnEditor).SelectedText + "; " +  
                            (editor as GridDropDownColumnEditor).SelectedValue;  
                        editorValue = (editor as GridDropDownColumnEditor).SelectedValue;  
                    }  
 
                    try  
                    {  
                        
 
                        DataRow[] changesRow = this.ExamData.Tables["Exam"].Select("semester_idsemester_id = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["semester_id"]);  
                        changesRow[0][column.UniqueName] = editorValue;  
                          
                        this.ExamData.Tables["Exam"].AcceptChanges();  
                    }  
                    catch (Exception ex)  
                    {  
                        RadGrid1.Controls.Add(new LiteralControl("<strong>Unable to set value of column '" + column.UniqueName + "'</strong> - " + ex.Message));  
                        e.Canceled = true;  
                        break;  
                    }  
                }  
            }  
        }  
    }  
}  
 

1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 11 Aug 2008, 05:26 AM
Hello Manuel,

The code in this example will perform in-memory update of the DataTable. You can check this code library if you want manual updates or you can use automatic updates with your SqlDataSource2.

Kind regards,
Vlad
the Telerik team

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