hi..
i have a gridview. i created two item TemplateField in gridview.
other two or more columns are added in gridview Dynamically using TemplateField of textbox.
<asp:GridView ID="grdProductFields" runat="server" AllowPaging="false"
AutoGenerateColumns="false" Width="100%" CssClass="mGrid" PageSize="50"
AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"
onrowdatabound="grdProductFields_RowDataBound"
onrowcommand="grdProductFields_RowCommand" >
<PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="First" LastPageText="Last"/>
<Columns>
<asp:TemplateField HeaderText="S. No." HeaderStyle-Width="40" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblSNo" runat="server" Text='<%# ((int)DataBinder.Eval(Container, "RowIndex"))+1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order No." HeaderStyle-Width="100">
<ItemTemplate>
<asp:TextBox ID="txtSerialno" runat="server" class="textbox" style="width:90px; vertical-align:top; margin-bottom:5px;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
i am adding dynamic column with textbox using follwing code :
foreach (DataColumn col in dtTemp.Columns)
{
TemplateField bfield = new TemplateField();
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, GridViewTemplate.ItemType.Textbox, col.ColumnName);
grdProductFields.Columns.Add(bfield);
}
grdProductFields.DataSource = dt;
grdProductFields.DataBind();
using GridViewTemplate class for create template field.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class GridViewTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;
public GridViewTemplate(ListItemType type, string colname)
{
_templateType = type;
_columnName = colname;
}
public GridViewTemplate(ListItemType type, ItemType itemtype , string colname)
{
_templateType = type;
_columnName = colname;
}
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 txt = new TextBox();
txt.DataBinding += new EventHandler(txt_DataBinding);
txt.Width = 90;
txt.CssClass = "textbox";
container.Controls.Add(txt);
break;
case ListItemType.EditItem:
break;
case ListItemType.Footer:
CheckBox chkColumn = new CheckBox();
chkColumn.ID = "Chk" + _columnName;
container.Controls.Add(chkColumn);
break;
}
}
}
on button click.., how to get the each textbox value to store in database..
i try some code but, this give error..
this code not recognize dynamically added columns in code behind..
normally cell index 0 or 1 are accessible this code but when we going to access next cell who is added dynamically give error.
foreach (GridViewRow grdrow in grdProductFields.Rows)
{
TextBox txt1 = null;
foreach (Control c in grdrow.Cells[2].Controls)
{
if (c != null)
{
if (c.GetType() == typeof(TextBox))
{
txt1 = (TextBox)c;
string str = txt1.Text;
}
string strtype = c.GetType().ToString();
}
}
}
please give me solution..