Hiii,
I am trying to generate gridview dynamically with textbox and button controls.
I have referred follo links.
I have binded dropdown of Material Type
On selected indexchanged event of dropdown i want to show gridview
i.e if Mobile has Parameter like 1.MAKE,2.MODEL,3.IMEI NUM then grid should popup with column name like MAKE ,MODEL ,IMEI NUM ALONG WITH TEXTBOX IN A ROW AND ONE MORE EXTRA COLUMN WITH NAME LIKE "ACTION" AND ITS COLUMN VALUE WILL BE BUTTON "SAVE"
AND IN A FOOTER ROW GRID CAN HAVE BUTTON TO ADD ROWS IN IT.
https://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx
https://www.aspsnippets.com/Articles/Dynamically-add-BoundField-and-TemplateField-Columns-to-GridView-in-ASPNet.aspx
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-12 col-lg-12">
<asp:GridView ID="grdMaterial" CssClass="table table-condensed table-bordered" Width="100%" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" OnRowDataBound="grdMaterial_RowDataBound" ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="SAVE" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAddRow" runat="server" Text="Add Row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
try
{
string query = "Select distinct t.Material_Type_Desc,(select p.Material_Parameter_desc from MST_MATERIAL_PARAMETER p where p.MATERIAL_PARAMETER_ID = l.Material_Parameter_ID ) as ParameterDesc from LINK_MAT_PARAMETER_TYPE l ,MST_MATERIAL_TYPE t where t.Material_Type_Id='" + drpMaterialType.SelectedValue + "'";
OleDbCommand cmd = new OleDbCommand(query, VMScon);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dtMaterial = new DataTable();
da.Fill(dtMaterial);
DataTable dt = new DataTable();
grdMaterial.DataSource = null;
dt.Clear();
grdMaterial.Columns.Clear();
for (int i = 0; i < dtMaterial.Rows.Count; i++)
{
TemplateField tfield = new TemplateField();
tfield.HeaderText = dtMaterial.Rows[i]["ParameterDesc"].ToString();
grdMaterial.Columns.Add(tfield);
}
for (int i = 0; i < dtMaterial.Rows.Count; i++)
{
dt.Columns.Add();
}
dt.Columns.Add("Add Row");
//for (int i = 0; i < dt.Columns.Count; i++)
for (int i = 0; i < 1; i++)
{
dt.Rows.Add();
}
grdMaterial.DataSource = dt;
grdMaterial.DataBind();
}
catch (Exception)
{
throw;
}
protected void grdMaterial_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < grdMaterial.Columns.Count; i++)
{
int rowID = e.Row.RowIndex;
TextBox textbox= new TextBox();
textbox.CssClass = "form-control input-sm";
txtCountry.ID = grdMaterial.Columns[i].ToString();
e.Row.Cells[i].Controls.Add(textbox);
}
}
I have follo table :
MST_MATERIAL_TYPE = MATERIAL_TYPE_Id,MATERIAL_TYPE_Desc
1 LAPTOP
2.MOBILE
3.SWITCH
4.CD
MST_MATERIAL_PARAMETER = MATERIAL_PARAMETER_ID,MATERIAL_PARAMETER_DEC
1.MODEL
2.MAKE
3.SERIAL NUM
4.IMEI NUM
LINK_MAT_PARAMETER_TYPE = MATERIAL_TYPE_Id,MATERIAL_PARAMETER_ID
1 - 1
1 - 2
1 - 3
2 - 1
2 - 2
2 - 4