I would like to add controls like textBox in GridView from code on the fly.
In my project i have one Grid in that i can't decide how many rows and columns are there. so that i just give it DataSource. This working fine.
GridView G = new GridView();
G.DataSourse = dt;
G.DataBind();
now i want to do such thing that in Gridview all the controls are Textbox control so that i can write in that textbox.
TextBox t= new TextBox();
G.Contorls.Add(t);
This will throw exception...
do anyone have any idea about this???
Thanks in advance..
Regards
Amit Vyas
Why not do it in design time with ItemTemplate
<asp:GridView ID="GrdDynamic" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="Name" Text='<%#Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
EDIT
Here is an interesting CodeProject post on dynamically adding template columns
Please check this http://www.devasp.net/net/articles/display/708.html link and use below code:
DropDownList ddl = new DropDownList();
ddl.Visible = true;
ddl.ID = "ddl1";
ddl.Items.Add("Item1");
TableCell cell = new TableCell();
gv.Rows[0].Cells.Add(cell);
gv.Rows[0].Cells[0].Controls.Add(ddl);
If you are looking a way to add TextBox dynamically in the existing GridView, then using RowDataBound event of the GridView would be best solution.
Add a PlaceHolder control in the ItemTemplate field.
<asp:GridView ID="GrdDynamic" runat="server" OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder runat='server' ID="PlaceHolder1"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
//find placeholder control
PlaceHolder placeHolder = e.Row.FindControl("PlaceHolder1") as PlaceHolder;
TextBox TextBox1 = new TextBox();
placeHolder.Controls.Add(TextBox1);
}
Related
GOAL:
I would like to update the status of the checkbox in the database when OnCheckedChanged event of the Checkbox fires. This checkbox resides on each row of gridview. Don't want to postback the whole page so I have the gridview inside a Updatepanel.
PROBLEM:
I can't get the OnCheckedChanged event to fire once the gridview is placed in a updatepanel.
or am I approaching this the wrong way?
Here is what i have for the updatepanel,gridview,checkbox and checkbox event code
The Binding of this gridview is within a if (!IsPostBack)
Even though it is not shown in this example below, that Gridview is nested in another gridview, if that makes a difference.
HTML
<asp:UpdatePanel ID="gridUpdatePanel" UpdateMode="Conditional" runat="server" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:GridView ID="gvComponents" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid" OnRowDataBound="gvComponents_RowDataBound" ShowHeader="false">
<Columns>
//Other TemplateFields
<asp:TemplateField HeaderText="Revisions Required" Visible="false" ItemStyle-Width="10%" >
<ItemTemplate>
<div style="text-align:center;">
<asp:CheckBox ID="cbREVISION_REQD" runat="server" Enabled="true" Checked='<%# (bool)Eval("REVISION") %>' AutoPostBack="true" OnCheckedChanged ="cbREVISION_CheckChanged" />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
C#
protected void cbREVISION_CheckChanged(object sender, EventArgs e)
{
//code to update the database
gridUpdatePanel.Update();
}
There seem to be nothing wrong with the approach! But, is
UpdateMode="Conditional"
a problem?
You have to try this for binding nested-gridview in cbREVISION_CheckChanged event:
protected void cbREVISION_CheckChanged(object sender, EventArgs e)
{
GridViewRow gvr = ((CheckBox)sender).Parent as GridViewRow; // gets the gridview row where checkbox cliked
GridView gv = gvr.Parent as GridView; // gets the cliked gridview or nested gridview
CheckBox chkbox = gvr.FindControl("cbREVISION_REQD") as CheckBox; // gets checkbox from cliked gridview
bool status = chkbox.Checked; // gets the status of the checkbox
// here bind your nested-gridview
gv.DataSource = dt; // dt is some data to which you set gridview
gv.DataBind(); // binding methed to bind gridview
}
It will work!
I have written code like below lines of code
protected void grdView_DataBinding(object sender, EventArgs e)
{
LicenceBL lbl = new LicenceBL(0);
DataSet lds = new DataSet();
lbl.FetchForEdit(lds, LicenseType);
foreach (GridViewRow row in grdView.Rows)
{
Label lblJurisdiction = row.FindControl("lblJurisdiction") as Label;
TextBox txtDateIssued = row.FindControl("txtEffectiveDate") as TextBox;
TextBox txtDateExpiration = row.FindControl("txtExpirationDate") as TextBox;
TextBox txtLicenseNumber = row.FindControl("txtLicenseNumber") as TextBox;
for (int i = 0; i < lds.Tables[0].Rows.Count; i++)
{
txtLicenseNumber.Text = lds.Tables[0].Rows[i]["LicenceNumber"].ToString();
}
}
}
I want to bind grid view without using datasource property of gridview. The above code is not working...
Let's Suppose lds contains data like
=====================================================
LicenceNumber - LicenceIssueDate
123 - 12/10/2014
345 - 12/1/2013
=====================================================
Similarily Grid will also contain the data
=====================================================
LicenceNumber - LicenceIssueDate
123 - 12/10/2014
345 - 12/1/2013
=====================================================
Here is grid view's design
<asp:GridView ID="grdView" AutoGenerateColumns="false" OnDataBinding="grdView_DataBinding" BorderWidth="0" runat="server" CssClass="table">
<Columns>
<asp:TemplateField HeaderText="License Number">
<ItemTemplate>
<asp:TextBox ID="txtLicenseNumber" style="padding:12px 5px;" runat="server" />
<br />
<asp:RequiredFieldValidator ID="ValReqLN" Display="Dynamic" runat="server"
ErrorMessage="License Number cannot be Blank." ControlToValidate="txtEffectiveDate"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Effective Date">
<ItemTemplate>
<asp:TextBox ID="txtEffectiveDate" style="padding:12px 5px;" placeholder="(mm/dd/yyyy)" CssClass="datepiker" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Please help!!!
Why would you not use datasource property ? you said: "i want to bind gridview programically because I have to give many conditions inside gridview's rows according to business logic in order to display data in the gridview.
Here is how you can do that:
Code behind:
protected void grdView_DataBinding(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (drv["txtLicenseNumber"].ToString().ToLower() == "abc")
{
//Apply business logic here on each row, hide/show etc
e.Row.CssClass = "highlighted";
}
}
}
Read more here on:
Dynamically change GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net
Selectively apply css to a row in a gridview
i have a Textbox inside ItemTemplate of a Gridview.. i need to find that textbox without using RowDatabound or any other event of Gridview
aspx
<asp:GridView ID="gv_Others" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate >
<asp:TextBox ID="txtEmp" runat="server" Width=100% Height=22px CssClass="input"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs
protected void Insert_OtherServices()
{
dsJobCardTableAdapters.Select_OtherServiceTableAdapter dsother = new dsJobCardTableAdapters.Select_OtherServiceTableAdapter();
int count = 0;
foreach (GridViewRow row in gv_Others.Rows)
{
//string b = (row.Cells[0].Controls[0] as DataBoundLiteralControl).Text;
//TextBox a = gv_Others.Rows[count].Cells[0].FindControl("txtEmp") as TextBox;
// string test = a.Text;
//TextBox other = (TextBox)gv_Others.Rows[count].FindControl("txtEmp");
TextBox other = (TextBox)gv_Others.TemplateControl.FindControl("txtEmp");
dsother.Insert_OtherService(other.Text);
count++;
}
}
is there any alternative to get the value from textbox which is inside the item template of Gridview
Do you mean that you want to find text box values without looping through GridView rows? If is is the case, jQuery/javascript can be used to achieve this.
Read all text box values on the client-side. You can find more information on how to do this here
Pass these values along with the postback request using a hidden field. Read and use the hidden field value inside your method.
I have this ASP:GridView and I cant seem to be able to declare the asp controls or div tags inside this ASP:GridView in my codebehind ascx file.
<asp:GridView runat="server" ID="siteMembersView" AllowPaging="True"
ShowHeader="False" EnableModelValidation="True" AutoGenerateColumns="false" CssClass="membership-gridview" Width="100%" CellPadding="0" CellSpacing="0">
<Columns>
<asp:TemplateField>
<ItemTemplate>
//content
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am trying to put Mydiv.Visible = false and LinkButton.Visible = False
But it cant find the IDs. I am using runat="server". I think the problem is that its inside the GridView beacuse I tried to put it outside the GridView and it worked perfectly.
Any kind of help is appreciated
Use the FindControl method to access your controls, then you can apply the logic to toggle the visibility of each:
How to find control in TemplateField of GridView?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
HtmlGenericControl myDiv;
LinkButton myLinkButton;
if(e.Row.RowType == DataControlRowType.DataRow)
{
myDiv = (HtmlGenericControl)e.Row.FindControl("myDiv")
as HtmlGenericControl;
myDiv.Visible = false;
myLinkButton = (LinkButton )e.Row.FindControl("myLinkButton")
as LinkButton;
myLinkButton.Visible = false;
}
}
I have two textbox and one gridview in my webform. The gridview is binded with database. But I want to add one more column on runtime which will be the textbox input from the webform. Because the scenario is like: I am maintaining two formula to calculate some percentage using the two textbox and the client wants to see this calculation for each row in the gridview.
But I cannot do this.
Is there anyone who can help me on this please? May be some suggestion.
Thanks in advance.
You could add the column in your GridView markup with a label control to display the result as follows.
Here is the markup needed, please note Visible is set to false.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="label1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use the RowDataBound event to find the label and calculate your result as below:
void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find the control
var label1 = e.Item.FindControl("label1") as Label;
if (label1 != null)
{
if (!string.IsNullOrEmpty(tbInput1.Text) && !string.IsNullOrEmpty(tbInput2.Text))
{
// Do the calculation and set the label
label1.Text = tbInput1.Text + tbInput2.Text;
// Make the column visible
GridView1.Columns[0].Visible = true;
}
}
}
}
Please forgive any errors, I have not tested the above.