问题描述
public static GridView CreateGridView(GridView objGridView,DataSet objDataSet)
{
objGridView.Columns.Clear();
// GridView objGridView = new GridView();
objGridView.AutoGenerateColumns = false;
objGridView.AllowSorting = true;
DataTable objDataTable = objDataSet.Tables [0];
if(objDataTable!= null)
{
TemplateField objTemplateFieldEdit = new TemplateField();
objTemplateFieldEdit.ItemTemplate = new GridViewTemplate(ListItemType.AlternatingItem,Edit,objDataTable);
TemplateField objTemplateFieldDelete = new TemplateField();
objTemplateFieldDelete.ItemTemplate = new GridViewTemplate(ListItemType.AlternatingItem,Delete,objDataTable);
objGridView.Columns.Add(objTemplateField编辑);
objGridView.Columns.Add(objTemplateFieldDelete);
objGridView.RowDataBound + = objGridView_RowDataBound;
for(int i = 0;我< objDataTable.Columns.Count; i ++)
{
TemplateField objTemplateField = new TemplateField();
objTemplateField.HeaderTemplate = new GridViewTemplate( ListItemType.Header,objDataTable.Columns [i] .ColumnName.ToString(),objDataTable);
objTemplateField.ItemTemplate = new GridViewTemplate(ListItemType.Item,objDataTable.Columns [i] .ColumnName.ToString( ),objDataTable);
objTemplateField.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem,objDataTable.Columns [i] .ColumnName.ToString(),objDataTable);
objGridView。 Columns.Add(objTemplateField);
}
objGridView.Sorting + = objGridView_Sorting;
objGridView.DataSource = null;
objGridView.DataSource = objDataTable;
string arr =(objDataTable.Columns [0] .ColumnName);
objGridView.DataKeyNames = new string [] {arr };
objGridView.DataBind();
}
返回objGridView;
公共类GridViewTemplate:ITemplate
{
ListItemType _templateType;
string _columnName;
DataTable _objDataTable;
public GridViewTemplate(ListItemType类型,字符串colname,DataTable objDataTable)
{
_templateType = type;
_columnName = colname;
_objDataTable = objDataTable;
}
void ITemplate.InstantiateIn(System.Web.UI.Control c ontainer)
{
开关(_templateType)
{
案例ListItemType.Header:
LinkButton objLinkButton = new LinkButton();
objLinkButton.Text = _columnName;
objLinkButton.ForeColor = System.Drawing.Color.Wheat;
objLinkButton.CommandName =Sort;
objLinkButton.CommandArgument = _columnName;
DropDownCheckBoxes objDropDownCheckBoxes = new DropDownCheckBoxes();
objDropDownCheckBoxes .ID = _columnName;
objDropDownCheckBoxes.DataSource = _objDataTable;
objDropDownCheckBoxes.DataTextField = _columnName;
objDropDownCheckBoxes.DataValueField = _columnName;
objDropDownCheckBoxes.UseSelectAllNode = false;
objDropDownCh eckBoxes.AutoPostBack = true;
objDropDownCheckBoxes.SelectedIndexChanged + = objDropDownCheckBoxes_SelectedIndexChanged;
objDropDownCheckBoxes.DataBind();
container.Controls.Add(objLinkButton);
container.Controls.Add(objDropDownCheckBoxes);
break;
case ListItemType.Item:
Label objLabel = new Label();
objLabel.DataBinding + = objLabel_DataBinding;
container.Controls。添加(objLabel);
break;
case ListItemType.EditItem:
TextBox tb2 = new TextBox();
tb2.DataBinding + = new EventHandler(tb1_DataBinding);
tb2.Columns = 4;
container.Controls.Add(tb2);
break;
case ListItemType.Footer:
CheckBox chkColumn = new CheckBox();
chkColumn.ID =Chk+ _columnName ;
container.Controls.Add(chkCo lumn);
break;
case ListItemType.AlternatingItem:
Button objButton = new Button();
objButton.Text = _columnName;
objButton.CommandName =cmd+ _ columnName;
container.Controls.Add(objButton);
休息;
}
}
public static DataTable filterDataTable {get;
void objDropDownCheckBoxes_SelectedIndexChanged(object sender,EventArgs e)
b $ b DataSet objDataSet1 = new DataSet();
DataTable objDataTable1 = new DataTable();
foreach(ListItem项目(发送者为ListControl).Items)
{
if(item.Selected)
{
DynamicBal objDynamicBal = new DynamicBal();
DataSet objDataSet = new DataSet();
objDataSet = objDynamicBal.Bind();
DataTable objDataTable = objDataSet.Tables [0];
if(objDataTable!= null)
{
DataView objDataView = new DataView(objDataTable);
objDataView.RowFilter = _columnName +=+ item.Text;
objDataTable1 = objDataView.ToTable();
}
}
}
filterDataTable = objDataTable1; // tz方法我在pageload中调用基于下拉选择项的数据绑定
}
void objLabel_DataBinding(object sender,EventArgs e)
{
标签objLabel =(标签)发件人;
GridViewRow容器=(GridViewRow)objLabel.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem,_columnName);
if(dataValue!= DBNull.Value)
{
objLabel .Text = dataValue.ToString();
}
}
void tb1_DataBinding(object sender,EventArgs e)
{
TextBox txtdata =(TextBox)发送者;
GridViewRow容器=(GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder。 Eval(container.DataItem,_columnName);
if(dataValue!= DBNull.Value)
{
txtdata.Tex t = dataValue.ToString();
}
}
}
}
在我的aspx页面中我打电话给tz
public static GridView CreateGridView(GridView objGridView, DataSet objDataSet)
{
objGridView.Columns.Clear();
//GridView objGridView = new GridView();
objGridView.AutoGenerateColumns = false;
objGridView.AllowSorting = true;
DataTable objDataTable = objDataSet.Tables[0];
if (objDataTable != null)
{
TemplateField objTemplateFieldEdit = new TemplateField();
objTemplateFieldEdit.ItemTemplate = new GridViewTemplate(ListItemType.AlternatingItem, "Edit", objDataTable);
TemplateField objTemplateFieldDelete = new TemplateField();
objTemplateFieldDelete.ItemTemplate = new GridViewTemplate(ListItemType.AlternatingItem, "Delete", objDataTable);
objGridView.Columns.Add(objTemplateFieldEdit);
objGridView.Columns.Add(objTemplateFieldDelete);
objGridView.RowDataBound += objGridView_RowDataBound;
for (int i = 0; i < objDataTable.Columns.Count; i++)
{
TemplateField objTemplateField = new TemplateField();
objTemplateField.HeaderTemplate = new GridViewTemplate(ListItemType.Header, objDataTable.Columns[i].ColumnName.ToString(), objDataTable);
objTemplateField.ItemTemplate = new GridViewTemplate(ListItemType.Item, objDataTable.Columns[i].ColumnName.ToString(), objDataTable);
objTemplateField.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem , objDataTable.Columns[i].ColumnName.ToString(), objDataTable);
objGridView.Columns.Add(objTemplateField);
}
objGridView.Sorting += objGridView_Sorting;
objGridView.DataSource = null;
objGridView.DataSource = objDataTable;
string arr = (objDataTable.Columns[0].ColumnName);
objGridView.DataKeyNames = new string[] { arr};
objGridView.DataBind();
}
return objGridView;
public class GridViewTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
DataTable _objDataTable;
public GridViewTemplate(ListItemType type, string colname, DataTable objDataTable)
{
_templateType = type;
_columnName = colname;
_objDataTable = objDataTable;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
LinkButton objLinkButton = new LinkButton();
objLinkButton.Text = _columnName;
objLinkButton.ForeColor = System.Drawing.Color.Wheat;
objLinkButton.CommandName = "Sort";
objLinkButton.CommandArgument = _columnName;
DropDownCheckBoxes objDropDownCheckBoxes = new DropDownCheckBoxes();
objDropDownCheckBoxes.ID = _columnName;
objDropDownCheckBoxes.DataSource = _objDataTable;
objDropDownCheckBoxes.DataTextField = _columnName;
objDropDownCheckBoxes.DataValueField = _columnName;
objDropDownCheckBoxes.UseSelectAllNode = false;
objDropDownCheckBoxes.AutoPostBack = true;
objDropDownCheckBoxes.SelectedIndexChanged += objDropDownCheckBoxes_SelectedIndexChanged;
objDropDownCheckBoxes.DataBind();
container.Controls.Add(objLinkButton);
container.Controls.Add(objDropDownCheckBoxes);
break;
case ListItemType.Item:
Label objLabel = new Label();
objLabel.DataBinding += objLabel_DataBinding;
container.Controls.Add(objLabel);
break;
case ListItemType.EditItem:
TextBox tb2 = new TextBox();
tb2.DataBinding += new EventHandler(tb1_DataBinding);
tb2.Columns = 4;
container.Controls.Add(tb2);
break;
case ListItemType.Footer:
CheckBox chkColumn = new CheckBox();
chkColumn.ID = "Chk" + _columnName;
container.Controls.Add(chkColumn);
break;
case ListItemType.AlternatingItem:
Button objButton = new Button();
objButton.Text = _columnName;
objButton.CommandName = "cmd"+_columnName;
container.Controls.Add(objButton);
break;
}
}
public static DataTable filterDataTable{get;set;}
void objDropDownCheckBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet objDataSet1 = new DataSet();
DataTable objDataTable1 = new DataTable();
foreach (ListItem item in (sender as ListControl).Items)
{
if (item.Selected)
{
DynamicBal objDynamicBal = new DynamicBal();
DataSet objDataSet = new DataSet();
objDataSet = objDynamicBal.Bind();
DataTable objDataTable = objDataSet.Tables[0];
if (objDataTable != null)
{
DataView objDataView = new DataView(objDataTable);
objDataView.RowFilter = _columnName + "=" + item.Text;
objDataTable1 = objDataView.ToTable();
}
}
}
filterDataTable = objDataTable1; //tz method im calling in pageload for data bind based on dropdown selected items
}
void objLabel_DataBinding(object sender, EventArgs e)
{
Label objLabel = (Label)sender;
GridViewRow container = (GridViewRow)objLabel.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
objLabel.Text = dataValue.ToString();
}
}
void tb1_DataBinding(object sender, EventArgs e)
{
TextBox txtdata = (TextBox)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}
}
}
}
in my aspx page im calling like tz
protected void Page_Load(object sender, EventArgs e)
{
//tz is for dropdown filtereing data to bind grid
if (GridViewTemplate.filterDataTable != null && GridViewTemplate.filterDataTable.Rows.Count > 0)
{
objDataSet = new DataSet();
objDataSet.Tables.Add(GridViewTemplate.filterDataTable);
GenerateDynamicGrid.CreateGridView(DynamicGridView, objDataSet);
}
else
{//tz is normal in pageload
objDataSet = new DataSet();
objDataSet = objDynamicBal.Bind();
GenerateDynamicGrid.CreateGridView(DynamicGridView, objDataSet);
}
}
但我无法获得网格im filtering ..
but im not able to get the grid what im filtering..
推荐答案
这篇关于无法根据gridview标头中的动态下拉列表将数据绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!