Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have the example code behind below


C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
   {

   }

   private string ConvertSortDirectionToSql(SortDirection sortDirection)
   {
       string newSortDirection = String.Empty;

       if (sortAscending)
           newSortDirection = "ASC";
       else
           newSortDirection = "DESC";

       sortAscending = !sortAscending;
       return newSortDirection;
   }

   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       GridView1.DataSource = SourceTable;
       GridView1.PageIndex = e.NewPageIndex;
       GridView1.DataBind();
   }

   protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       //Delete selected row.
       int SignId = (int)GridView1.DataKeys[e.RowIndex].Value;
       if (SignId == 0) return;

       // SignManager.DeleteSign(SignId);
   }

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName.CompareTo("") == 0)
       {
           Guid id = ((Guid)GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
           if (id == Guid.Empty) return;
       }
   }

   protected void SortGridView(string sortExpression, string direction)
   {
       DataTable dataTable = SourceTable;

       if (dataTable != null)
       {
           DataView dataView = new DataView(dataTable);
           dataView.Sort = sortExpression + " " + direction;

           GridView1.DataSource = dataView;
           GridView1.DataBind();
           int pageindex = GridView1.PageIndex;

       }
   }

   protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
   {
       isSort = true;

       string sortExpression = e.SortExpression;
       ViewState["SortExpression"] = sortExpression;

       if (GridViewSortDirection == SortDirection.Ascending)
       {
           isAscend = true;
           SortGridView(sortExpression, "ASC");
           GridViewSortDirection = SortDirection.Descending;
       }
       else
       {
           isAscend = false;
           SortGridView(sortExpression, "DESC");
           GridViewSortDirection = SortDirection.Ascending;
       }
   }


   public static DataTable CreateTable<T>()
   {
       Type entityType = typeof(T);
       DataTable table = new DataTable(entityType.Name);
       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

       foreach (PropertyDescriptor prop in properties)
       {
           table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
       }
       return table;
   }

   public static DataTable ConvertTo<T>(IList<T> list)
   {
       DataTable table = CreateTable<T>();
       Type entityType = typeof(T);
       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

       foreach (T item in list)
       {
           DataRow row = table.NewRow();

           foreach (PropertyDescriptor prop in properties)
           {
               row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
           }
           table.Rows.Add(row);
       }
       return table;
   }


Save Method

C#
public List<string> GetLevelSignsToBeSaved()
   {
       List<string> selectedIds = new List<string>();

       List<Stat> stats = new List<Stat>();

       DataColumn promoColumn = SourceTable.Columns["Column_PROMOTION"];
       DataColumn qtyColumn = SourceTable.Columns["Column_QTY"];
       DataColumn promo2Column = SourceTable.Columns["Column_ADDPROMO"];
       DataColumn qty2Column = SourceTable.Columns["Column_2QTY"];
       DataColumn printedColumn = SourceTable.Columns["Column_PRINT"];

       if (promoColumn != null || qtyColumn != null || promo2Column != null || qty2Column != null)
       {
           foreach (GridViewRow row in this.GridView1.Rows)
           {
               List<LevelSign> Signs = new List<LevelSign>();
               LevelSign LevelSign = new LevelSign();
               LevelSign secondSign = new LevelSign();

               if (promoColumn != null)
               {
                   if (row.Cells[promoColumn.Ordinal].Controls.Count > 0)
                   {
                       DropDownList ddlChanges = row.Cells[promoColumn.Ordinal].Controls[0] as DropDownList;
                       if (ddlChanges is DropDownList)
                       {
                           if (ddlChanges.SelectedValue != "" && Convert.ToInt32(ddlChanges.SelectedValue) != 0)
                           {
                               string levelSignId = ddlChanges.ID.Split('_')[2];
                               LevelSign = LevelManager.GetLevelSignByLevelSignIdSimple(Convert.ToInt32(levelSignId));
                               if (LevelSign.StampID == Convert.ToInt32(ddlChanges.SelectedValue))
                               {
                                   //do nothing
                               }
                               else
                               {
                                   LevelSign.StampID = Convert.ToInt32(ddlChanges.SelectedValue);
                                   stats.Add(StatManager.makeStat(LevelSign.SignID, LevelSign.LevelID, LevelSign.LevelID, LevelSign.sign.DepartmentID, LevelSign.StampID, (int)tsUtil.stattype.vChangeSizeAtMatrix, DateTime.Now, LevelSign.LevelSignQuantity));
                               }
                           }
                       }
                   }
               }

              if (qtyColumn != null)
               {
                   if (row.Cells[qtyColumn.Ordinal].Controls.Count > 0)
                   {
                       DropDownList ddlChanges = row.Cells[qtyColumn.Ordinal].Controls[0] as DropDownList;
                       if (ddlChanges is DropDownList)
                       {
                           if (ddlChanges.SelectedValue != "" && Convert.ToInt32(ddlChanges.SelectedValue) != 0)
                           {
                               if (LevelSign.LevelSignQuantity == Convert.ToInt32(ddlChanges.SelectedValue))
                               {
                                   //do nothing
                               }
                               else
                               {
                                   LevelSign.LevelSignQuantity = Convert.ToInt32(ddlChanges.SelectedValue);
                                   stats.Add(StatManager.makeStat(LevelSign.SignID, LevelSign.LevelID, LevelSign.LevelID, LevelSign.sign.DepartmentID, LevelSign.sign.StampID, (int)tsUtil.stattype.vChangeQuantityAtMatrix, DateTime.Now, LevelSign.LevelSignQuantity));
                               }
                           }
                       }
                   }
               }


               if (promo2Column != null)
               {
                   if (row.Cells[promo2Column.Ordinal].Controls.Count > 0)
                   {
                       DropDownList ddlChanges = row.Cells[promo2Column.Ordinal].Controls[0] as DropDownList;
                       if (ddlChanges is DropDownList)
                       {
                           if (ddlChanges.SelectedValue != "" && Convert.ToInt32(ddlChanges.SelectedValue) != 0)
                           {
                               string levelSignId = ddlChanges.ID.Split('_')[2];
                               secondSign = LevelManager.GetLevelSignByLevelSignIdSimple(Convert.ToInt32(levelSignId));

                               secondSign.LevelSignID = -1;
                               secondSign.StampID = Convert.ToInt32(ddlChanges.SelectedValue);
                               stats.Add(StatManager.makeStat(secondSign.SignID, secondSign.LevelID, secondSign.LevelID, secondSign.sign.DepartmentID, secondSign.StampID, (int)tsUtil.stattype.vDuplicateSignAtMatrix, DateTime.Now, secondSign.LevelSignQuantity));
                           }
                       }
                   }
               }

               if (qty2Column != null)
               {
                   if (row.Cells[qty2Column.Ordinal].Controls.Count > 0)
                   {
                       DropDownList ddlChanges = row.Cells[qty2Column.Ordinal].Controls[0] as DropDownList;
                       if (ddlChanges is DropDownList)
                       {
                           if (ddlChanges.SelectedValue != "" && Convert.ToInt32(ddlChanges.SelectedValue) != 0)
                           {
                               secondSign.LevelSignQuantity = Convert.ToInt32(ddlChanges.SelectedValue);
                               if (secondSign.LevelID != 0)
                                   stats.Add(StatManager.makeStat(secondSign.SignID, secondSign.LevelID, secondSign.LevelID, secondSign.sign.DepartmentID, secondSign.StampID, (int)tsUtil.stattype.vAugmentQuantityAtMatrix, DateTime.Now, secondSign.LevelSignQuantity));
                               //Signs.Add(secondSign);
                           }
                       }
                   }
               }
               if (LevelSign.SignID != 0)
                   Signs.Add(LevelSign);
               if (secondSign.SignID != 0)
               {
                   Signs.Add(secondSign);
               }
               foreach (LevelSign sign in Signs)
               {
                   SignManager.SaveLevelSignQuick(sign);
                   if (printedColumn != null)
                   {
                       if (row.Cells[printedColumn.Ordinal].Controls.Count > 0)
                       {
                           CheckBox cbPrint = row.Cells[printedColumn.Ordinal].Controls[0] as CheckBox;
                           if (cbPrint is CheckBox)
                           {
                               if (cbPrint.Checked)
                               {
                                   //selectedIds.Add(cbPrint.ID);
                                   selectedIds.Add(Convert.ToString(sign.LevelSignID) + "_" + sign.SignID + "_" + sign.StampID);
                               }
                           }
                       }
                   }
               }
           }
           StatManager.SaveStats(stats);
       }

       return selectedIds;
   }




and my gridview has dynamically populated values of boundfields and templatefields. I am only enabling the user to sort on the boundfields. The sorting is working but there are drop down lists to save values in each row, so when those are changed after sorting it just returns the original value. Saving without sorting works as well. any help would be great
Posted
Updated 2-May-15 9:56am
v4
Comments
"it just returns the original value." - not getting this. Can you give one example?
Suvendu Shekhar Giri 1-May-15 19:16pm    
Where is the code to save data?
bobb024 2-May-15 15:58pm    
I added the save method above. Also for the original value comment, for instance, the row has a value of 1 in the drop down list, then I sort the list and change the quantity to 20, it is still saving the first value (ie 1)
When you select 20, it should show 20. How it is showing you 1?
bobb024 4-May-15 16:39pm    
it shows 20 visually but when I fire the button, it is not saving correctly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900