Creating Dynamic row in Gridview which contains Dropdown using C# with Asp.net
Grid View :
<asp:GridView ID="gdvSupplierAccess" runat="server" ShowFooter="true" Width="650px"
CellPadding="4" CssClass="GRD" AutoGenerateColumns="false">
<AlternatingRowStyle CssClass="GRDALTERNATEITEM" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="RowNumber" HeaderStyle-Width="210px"
Visible="false" SortExpression="UserID" />
<asp:TemplateField HeaderText="Select" HeaderStyle-Width="50px">
<ItemTemplate>
<asp:UpdatePanel ID="UpdSelect" runat="server">
<ContentTemplate>
<asp:CheckBox ID="ChkSelect" runat="server" CssClass="CheckBox" />
<asp:TextBox ID="txtRowNo" runat="server" Visible="false" Text='<%# Bind("RowNumber") %>'></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product1" HeaderStyle-Width="210px">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProduct1" runat="server" CssClass="DROPDOWN">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product2" HeaderStyle-Width="210px">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProduct2" runat="server" CssClass="DROPDOWN">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product3" HeaderStyle-Width="210px">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProduct3" runat="server" CssClass="DROPDOWN">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<FooterTemplate>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void BindGrid()
{
try
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Select", typeof(string)));
dt.Columns.Add(new DataColumn("Product1", typeof(string)));
dt.Columns.Add(new DataColumn("product2", typeof(string)));
dt.Columns.Add(new DataColumn("Product3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Product1"] = string.Empty;
dr["Product2"] = string.Empty;
dr["ProductType"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
gdvSupplierAccess.DataSource = dt;
gdvSupplierAccess.DataBind();
DropDownList ddlProduct1 = (DropDownList)gdvSupplierAccess.Rows[0].FindControl("ddlSupplier");
DropDownList ddlProduct2 = (DropDownList)gdvSupplierAccess.Rows[0].FindControl("ddlProduct2");
DropDownList ddlProduct3 = (DropDownList)gdvSupplierAccess.Rows[0].FindControl("ddlProductType");
BindSupplierInfo(ddlProduct1);
BindProduct2Info(ddlProduct2);
BindProductTypeInfo(ddlProduct);
}
catch (Exception ex)
{
}
}
protected void BindSupplierInfo(DropDownList ddl)
{
DataSet dsSupplier = new DataSet();
try
{
dsProduct1 = objSupp.BindProducts(); //fill the Data to dataset ds supplier
if (dsProduct1 .Tables.Count > 0)
{
if dsProduct1 .Tables[0].Rows.Count > 0)
{
ddl.DataTextField = "Product1";
ddl.DataValueField = "ProductCode";
ddl.DataSource = dsSupplier;
ddl.DataBind();
ddl.Items.Insert(0, "------Select------");
}
}
}
catch (Exception ex)
{
}
}
protected void BindProduct2Info(DropDownList ddl1)
{
DataSet dsProduct2 = new DataSet();
try
{
dsProduct2 = objSupp.BindProduct2(); //fill the Data to dataset dsProduct
if (dsProduct2.Tables.Count > 0)
{
if (dsProduct2.Tables[0].Rows.Count > 0)
{
ddl1.DataTextField = "Product2";
ddl1.DataValueField = "Product2No";
ddl1.DataSource = dsProduct2;
ddl1.DataBind();
ddl1.Items.Insert(0, "------Select------");
}
}
}
catch (Exception ex)
{
Response.Redirect("CustomErrorPage.aspx", false);
}
}
protected void BindProductTypeInfo(DropDownList ddl2)
{
DataSet dsProduct3 = new DataSet();
try
{
dsProduct3 = objSupp.BindProductTypes();
if (dsProduct3.Tables.Count > 0)
{
if (dsProduct3.Tables[0].Rows.Count > 0)
{
ddl2.DataTextField = "Product3";
ddl2.DataValueField = "Product3No";
ddl2.DataSource = dsProduct;
ddl2.DataBind();
ddl2.Items.Insert(0, "------Select------");
}
}
}
catch (Exception ex)
{
}
}
private void AddNewRow()
{
try
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
for (int j = 0; j < gdvSupplierAccess.Rows.Count; j++)
{
DropDownList ddlProduct1 = (DropDownList)gdvSupplierAccess.Rows[j].Cells[1].FindControl("ddlSupplier");
DropDownList ddlProduct2 = (DropDownList)gdvSupplierAccess.Rows[j].Cells[2].FindControl("ddlProduct2");
DropDownList ddlProduct3 = (DropDownList)gdvSupplierAccess.Rows[j].Cells[3].FindControl("ddlProductType");
dtCurrentTable.Rows[j]["Product1"] = ddlProduct1.SelectedItem.Text.ToString().Trim();
dtCurrentTable.Rows[j]["Product2"] = ddlProduct2.SelectedItem.Text.ToString().Trim();
dtCurrentTable.Rows[j]["Product3"] = ddlProduct3.SelectedItem.Text.ToString().Trim();
}
gdv.DataSource = dtCurrentTable;
gdv.DataBind();
}
}
else
{
}
SetPreviousData();
}
catch (Exception ex)
{
}
}
private void SetPreviousData()
{
int rowIndex = 0;
try
{
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
int introw = dt.Rows.Count;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddlProduct1 = (DropDownList)gdvSupplierAccess.Rows[rowIndex].Cells[1].FindControl("ddlSupplier");
DropDownList ddlProduct2 = (DropDownList)gdvSupplierAccess.Rows[rowIndex].Cells[2].FindControl("ddlProduct2");
DropDownList ddlProduct3 = (DropDownList)gdvSupplierAccess.Rows[rowIndex].Cells[3].FindControl("ddlProductType");
BindSupplierInfo(ddlProduct1);
BindProduct2Info(ddlProduct2);
BindProductTypeInfo(ddlProduct3);
if (i < dt.Rows.Count-1)
{
ddlProduct1.ClearSelection();
ddlProduct1.Items.FindByText(dt.Rows[i]["Product1"].ToString()).Selected = true;
ddlProduct2.ClearSelection();
ddlProduct2.Items.FindByText(dt.Rows[i]["Product2"].ToString()).Selected = true;
ddlProduct3.ClearSelection();
ddlProduct3.Items.FindByText(dt.Rows[i]["ProductType"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
catch (Exception ex)
{
}
}
protected void btnAddNewRow_Click(object sender, EventArgs e)
{
try
{
AddNewRow();
}
catch (Exception ex)
{
}
}
No comments :
Post a Comment