0
동적 응용 프로그램을 만들고 있습니다. 추가, 업데이트, 삭제, 페이징 및 정렬 옵션을 제공하는 곳. 나는 테이블 이름을 옵션으로 배치하는 최상위 드롭 다운에 하나 있습니다. 문제는 하나의 표를 선택하여 정렬하고 드롭 다운에서 표를 변경하여 열 A를 찾을 수없는 오류입니다 (이전 표에서 정렬을 수행 한 결과).다른 테이블로 데이터 테이블을 이미 정렬 된 gridview에 바인딩하는 방법
여기 내 코드가 있습니다. 데이터 요소는 데이터를 얻고, 두 번째는 데이터 바인딩 불리는 방법이다
protected void gv_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
string sortdir = string.Empty;
if (dir == SortDirection.Ascending)
{
dir = SortDirection.Descending;
sortdir = "Desc";
sortImage.ImageUrl = "../Images/asc.gif";
}
else
{
dir = SortDirection.Ascending;
sortdir = "Asc";
sortImage.ImageUrl = "../Images/desc.gif";
}
DataView sortedView = new DataView(dt);
sortString = e.SortExpression + " " + sortdir;
sortedView.Sort = sortString;
ViewState.Add("View", dt);
ViewState.Add("sortString", sortString);
gv.DataSource = sortedView;
gv.DataBind();
int columnIndex = 0;
foreach (DataControlFieldHeaderCell headerCell in gv.HeaderRow.Cells)
{
if (headerCell.ContainingField.SortExpression == e.SortExpression)
{
columnIndex = gv.HeaderRow.Cells.GetCellIndex(headerCell);
}
}
gv.HeaderRow.Cells[columnIndex].Controls.Add(sortImage);
}
catch (Exception ex)
{
AMP1.Title = "Unable to Get Data.!";
AMP1.Message = ex.Message;
AMP1.MessageDescription = ex.StackTrace;
AMP1.AppMessageType = TEMP.Controls.AplicationMessagePanel.MessageType.Error;
((Button)AMP1.FindControl("btnOK")).CausesValidation = false;
((Button)AMP1.FindControl("btnOK")).Focus();
ModalPopupExtenderException.Show();
}
}
public void createGrid(DataTable dtTable)
{
try
{
// setting flag to get first dropdownlist name
bool flag = true;
dtTable.Columns.Add("SrNo", typeof(String)).SetOrdinal(0);
//Clearing all the columns before adding new one
gv.Columns.Clear();
int count = gv.Columns.Count - 1;
// Creating Columns for the grid
for (int i = 0; i < dtTable.Columns.Count; i++)
{
string colName = dtTable.Columns[i].ColumnName;
TemplateField lname = new TemplateField();
if (HeaderText(colName) != "")
{
lname.HeaderText = HeaderText(colName);// +"<font color='red'>*</font>";
}
else
{
lname.HeaderText = colName;// +"<font color='red'>*</font>"; ;
}
if (colName != "SrNo")
{
lname.SortExpression = colName;
}
if (colName != "ID" && colName != "Active" && colName != "SrNo" && colName != "BModelValue_Val")
{
lname.ItemStyle.CssClass = "tr1";
lname.FooterStyle.CssClass = "tr2";
}
else
{
if (colName == "BModelValue_Val")
{
lname.ItemStyle.CssClass = "tdBeveWorm";
lname.FooterStyle.CssClass = "tdBeveWorm";
}
else
{
lname.ItemStyle.CssClass = "tr1";
lname.FooterStyle.CssClass = "tr2";
}
}
if (colName.EndsWith("_Val") || colName == "ID" || colName == "SrNo")
{
lname.ItemTemplate = new AddTemplateToGrid(AddTemplateToGrid.enumControlType.Label, colName);
if (colName.EndsWith("_Val") && flag == true)
{
firstddl = colName;
flag = false;
}
if (colName != "ID" && colName != "SrNo")
lname.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.DropDownList, colName);
}
else
{
if (colName == "Active")
{
lname.ItemTemplate = new AddTemplateToGrid(AddTemplateToGrid.enumControlType.CheckBox, colName);
lname.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.CheckBox, colName);
}
else
{
lname.ItemTemplate = new AddTemplateToGrid(AddTemplateToGrid.enumControlType.TextBox, colName);
lname.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.TextBox, colName);
}
}
gv.Columns.Add(lname);
}
// Command Buttons for Grid
TemplateField lname1 = new TemplateField();
lname1.HeaderText = "Action";
lname1.ItemTemplate = new AddTemplateToGrid("Item", AddTemplateToGrid.enumControlType.ImageButton, "Action");
lname1.FooterTemplate = new AddTemplateToGrid("footer", AddTemplateToGrid.enumControlType.ImageButton, "Action");
//lname1.FooterStyle.Width = new Unit("150px");
//lname1.ItemStyle.Width = new Unit("150px");
gv.Columns.Add(lname1);
gv.AutoGenerateColumns = false;
// Checking whether page is sorted or not
if (ViewState["View"] != null)
{
DataView dv1 = new DataView(dtTable);
dv1.Sort = ViewState["sortString"].ToString();
// Binding DataView to the Grid
gv.DataSource = dv1;
}
else
{
// Binding DataTable to the Grid
gv.DataSource = dtTable;
}
gv.DataBind();
//Checking Page is having Index or not and setting HiddenField Value.
if (dtTable.Rows.Count > gv.PageSize)
hdfPagindex.Value = "1";
else
hdfPagindex.Value = "0";
}
catch (Exception ex)
{
AMP1.Title = "Unable to Get Data.!";
AMP1.Message = ex.Message;
AMP1.MessageDescription = ex.StackTrace;
AMP1.AppMessageType = TEMP.Controls.AplicationMessagePanel.MessageType.Error;
((Button)AMP1.FindControl("btnOK")).CausesValidation = false;
((Button)AMP1.FindControl("btnOK")).Focus();
ModalPopupExtenderException.Show();
}
}
public void loadTable(string tblName)
{
// Getting Table records
dt = null;
dt = bl.BindDataToMaster(tblName);
// Creating grid according to DataTable
gv.Columns.Clear();
createGrid(dt);
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
ViewState["View"] = null;
ViewState["sortString"] = null;
tblName = ddl.SelectedItem.Value;
gv.PageIndex = 0;
loadTable(tblName);
}
catch (Exception ex)
{
AMP1.Title = "Unable to Get Data.!";
AMP1.Message = ex.Message;
AMP1.MessageDescription = ex.StackTrace;
AMP1.AppMessageType = WEIR_BDK.Controls.AplicationMessagePanel.MessageType.Error;
((Button)AMP1.FindControl("btnOK")).CausesValidation = false;
((Button)AMP1.FindControl("btnOK")).Focus();
ModalPopupExtenderException.Show();
}
} //DropDownList Index Changed