Monday, 12 August 2013

SAVING GRIDPANEL VALUES WITH DRAG AND DROP FACILITY TO DATABASE IN EXT.NET



In the previous post it is mentioned how to enable drag and drop feature in a gridpanel without any additional DLLs.

So first add a grid panel with drag and drop facility in aspx page.




<ext:GridPanel
            ID="GridPanel1"
            runat="server"
            StoreID="Store1"
            Title="Drag and drop to adjust Timetable"
            Collapsible="true"
            Cls="x-grid-custom"
            ForceFit="true">
            <ColumnModel ID="ColumnModel1" runat="server">
             <Columns>
                    <ext:Column ID="Column1" runat="server" Text="Monday" DataIndex="Monday" Width="75" />
                    <ext:Column ID="Column2" runat="server" Text="Tuesday" Width="75" DataIndex="Tuesday"/>
                    <ext:Column ID="Column3" runat="server" Text="Wednesday" Width="75" DataIndex="Wednesday" />
                    <ext:Column ID="Column4" runat="server" Text="Thursday" Width="75" DataIndex="Thursday" />
                     <ext:Column ID="Column5" runat="server" Text="Friday" Width="75" DataIndex="Friday" />
                     <ext:Column ID="Column6" runat="server" Text="Saturday" Width="75" DataIndex="Saturday" />
             </Columns>
            </ColumnModel>
         
              <View>
                <ext:GridView ID="GridView1" runat="server">  
                  <Plugins>                                                  
                   <ext:CellDragDrop ID="CellDragDrop1" runat="server" ApplyEmptyText="false" EnforceType="true" />    
                  </Plugins>                    
                </ext:GridView>            
             </View>                  
</ext:GridPanel>
         <ext:Label ID="Label1" runat="server" Hidden="true" /><br />
         <ext:Label ID="Label2" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label3" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label4" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label5" runat="server" Hidden="true"/><br />
         <ext:Label ID="Label6" runat="server" Hidden="true"/>




The labels are temporary storage locations and therefore you have to give the same number of Labels as that of the columns.Here I use 6 Labels since I had 6 columns.
Now at the code behind section give the following code.




     protected void SubmitSelection(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["Values"];
            Dictionary<string, string>[] companies = JSON.Deserialize<Dictionary<string, string>[]>(json);
            foreach (Dictionary<string, string> row in companies)
            {
                int i = 1;
                StringBuilder sb = new StringBuilder();
                StringBuilder sb1 = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                StringBuilder sb3 = new StringBuilder();
                StringBuilder sb4 = new StringBuilder();
                StringBuilder sb5 = new StringBuilder();
                foreach (KeyValuePair<string, string> keyValuePair in row)
                {
                    if (i == 1)
                    {
                        sb.Append(keyValuePair.Value);
                    }
                    else if (i == 2)
                        sb1.Append(keyValuePair.Value);
                    else if (i == 3)
                        sb2.Append(keyValuePair.Value);
                    else if (i == 4)
                        sb3.Append(keyValuePair.Value);
                    else if (i == 5)
                        sb4.Append(keyValuePair.Value);
                    else if(i == 6)
                        sb5.Append(keyValuePair.Value);
                    i++;
                }
                this.Label1.Text = sb.ToString();
                this.Label2.Text = sb1.ToString();
                this.Label3.Text = sb2.ToString();
                this.Label4.Text = sb3.ToString();
                this.Label5.Text = sb4.ToString();
                this.Label6.Text = sb5.ToString();
                SqlConnection con = new SqlConnection("YOUR CONNECTIONSTRING");
                string query = "insert into tablename 0values ('" + Label1.Text + "','" + Label2.Text + "','" + Label3.Text + "','" + Label4.Text + "','" + Label5.Text + "','" + Label6.Text + "')";
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



In this the 'i' value represents the number of columns.



0 comments:

Post a Comment