Hello,
I have the example code behind below
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)
{
int SignId = (int)GridView1.DataKeys[e.RowIndex].Value;
if (SignId == 0) return;
}
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
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))
{
}
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))
{
}
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));
}
}
}
}
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(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