Create Template//********************************
public class GridViewTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
string _ControlID;
public GridViewTemplate(ListItemType type, string colname, string ID)
{
_templateType = type;
_columnName = colname;
_ControlID = ID;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
Label lbl = new Label();
lbl.Text = _columnName;
container.Controls.Add(lbl);
break;
case ListItemType.Item:
TextBox tb1 = new TextBox();
tb1.Width = 100;
tb1.MaxLength = 50;
tb1.ID = _ControlID;
tb1.DataBinding += new EventHandler(tb1_DataBinding);
//tb1.Columns = 4;
container.Controls.Add(tb1);
break;
case ListItemType.EditItem:
break;
//case ListItemType.Footer:
// CheckBox chkColumn = new CheckBox();
// chkColumn.ID = "Chk" + _columnName;
// container.Controls.Add(chkColumn);
// break;
}
}
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();
}
}
}
Show Columns//***********************
public void Show_DGV_GridView_Column()
{
try
{
DataTable dt = (DataTable)ViewState["dt"].Copy();
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i > 1)
{
DataColumn col = dt.Columns[i];
string gid;
gid = "TxtLanguage" + i.ToString();
TemplateField bfield = new TemplateField();
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName, gid);
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName, gid);
DGV_GridView.Columns.Add(bfield);
}
else
{
DataColumn col = dt.Columns[i];
if (DGV_GridView.Columns.Count > 1)
{
if (DGV_GridView.Columns[1].HeaderText != "HeaderText" && DGV_GridView.Columns[1].HeaderText != "HeaderText")
{
BoundField bfield = new BoundField();
bfield.HeaderText = col.ColumnName;
bfield.DataField = col.ColumnName;
bfield.ItemStyle.Width = 200;
DGV_GridView.Columns.Add(bfield);
}
}
else
{
BoundField bfield = new BoundField();
bfield.HeaderText = col.ColumnName;
bfield.DataField = col.ColumnName;
bfield.ItemStyle.Width = 200;
DGV_GridView.Columns.Add(bfield);
}
}
}
DGV_GridView.AutoGenerateColumns = false;
DGV_GridView.DataSource = dt;
DGV_GridView.DataBind();
}
catch (Exception ex)
{
}
}
No comments:
Post a Comment