You must Sign In to post a response.
  • Category: .NET

    Bind Drop Down list within Grid View.......

    Dear All,

    I hv a Drop Down with Gridview with Item Template Column. Drop Down is binding List from Database (15 records are available). I hv Add Button In Grid Footer to add More Row in Grid. My Requirement is, When Any new Row is Added by Add Button In Grid Footer, Then In Drop Down list, Previous all rows Item of Drop Down, should not be Bind or if Bind, then User Can'ot select that Item in Drop Down List. I m googling but not find yet..... below is my Code.....

    <asp:GridView ID ="grdWSPlanning" AutoGenerateColumns="false" CellPadding="3" runat="server" Visible="false" ShowFooter="true" OnRowDataBound="grdWSPlanning_RowDataBound" OnRowDeleting="grdWSPlanning_RowDeleting">

    <HeaderStyle CssClass="grdHeader" HorizontalAlign="Center" VerticalAlign="Middle" />
    <RowStyle CssClass="grdRow" HorizontalAlign="Center" VerticalAlign="Middle" Wrap="true" />
    <AlternatingRowStyle CssClass="grdAlternate" />
    <EmptyDataRowStyle CssClass="grdEmpty" HorizontalAlign="Center" VerticalAlign="Middle" />

    <FooterStyle BackColor="#866B6B" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

    <Columns>

    <asp:BoundField DataField="RowNumber" HeaderText="Sr. No." />
    <asp:TemplateField HeaderText="Project No.">
    <ItemStyle Width="5%" />
    <ItemTemplate>
    <asp:TextBox ID="gvtxtPRNO" EnableViewState="true" runat="server" CssClass="input_text_small_errors" Enabled="false" BackColor="#F1F1D6" ></asp:TextBox>
    </ItemTemplate>

    <FooterStyle HorizontalAlign="Right" />
    <FooterTemplate>
    <asp:Button ID="gvButtonAdd" runat="server" Text="Add WorkStages" CssClass="input_SaveLargbutton" OnClick="gvButtonAdd_Click"/>
    </FooterTemplate>

    </asp:TemplateField>












    <asp:TemplateField HeaderText="WorkStage">
    <ItemStyle Width="10%" />
    <ItemTemplate>
    <asp:DropDownList ID="gvddlWSNo" runat="server" ></asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>










    <asp:TemplateField HeaderText="WorkStage Start date">
    <ItemStyle Width="20%" />
    <ItemTemplate>
    <asp:TextBox ID="gvtxtWSSDATE" EnableViewState="true" runat="server" CssClass="input_text_small_errors" Enabled="false" BackColor="#F1F1D6" ></asp:TextBox>

    <CC1:TextBoxWatermarkExtender ID="gvtxtWSSDATE_TextBoxWatermarkExtender" runat="server" Enabled="True" TargetControlID="gvtxtWSSDATE" WatermarkText="DD/MM/YYYY"></CC1:TextBoxWatermarkExtender>
    <CC1:CalendarExtender ID="gvtxtWSSDATE_CalendarExtender1" TargetControlID="gvtxtWSSDATE" CssClass="cal_Theme1"
    PopupButtonID="gvtxtWSSDATE_TxtCalSImg1" runat="server" Format="dd/MM/yyyy" FirstDayOfWeek="Sunday" OnClientDateSelectionChanged="checkDate">
    </CC1:CalendarExtender>
    <asp:ImageButton ID="gvtxtWSSDATE_TxtCalSImg1" ImageUrl="../Resources/Images/calender.gif" Height="20px" Width="20px" runat="server" />

    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="WorkStage End Date">
    <ItemStyle Width="15%" />
    <ItemTemplate>
    <asp:TextBox ID="gvtxtWSEDATE" EnableViewState="true" runat="server" CssClass="input_text_small_errors" Enabled="false" BackColor="#F1F1D6" ></asp:TextBox>

    <CC1:TextBoxWatermarkExtender ID="gvtxtWSEDATE_TextBoxWatermarkExtender" runat="server" Enabled="True" TargetControlID="gvtxtWSEDATE" WatermarkText="DD/MM/YYYY"></CC1:TextBoxWatermarkExtender>
    <CC1:CalendarExtender ID="gvtxtWSSDATE_CalendarExtender2" TargetControlID="gvtxtWSEDATE" CssClass="cal_Theme1"
    PopupButtonID="gvtxtWSSDATE_TxtCalSImg2" runat="server" Format="dd/MM/yyyy" FirstDayOfWeek="Sunday" OnClientDateSelectionChanged="checkDatebetweenDates">
    </CC1:CalendarExtender>
    <asp:ImageButton ID="gvtxtWSSDATE_TxtCalSImg2" ImageUrl="../Resources/Images/calender.gif" Height="20px" Width="20px" runat="server" />

    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="WorkStage Completion (%)">
    <ItemStyle Width="10%" />
    <ItemTemplate>
    <asp:TextBox ID="gvtxtWSCOPLETION" EnableViewState="true" runat="server" CssClass="input_text_small_errors" BackColor="#F1F1D6" onkeypress="return OnlyNumeric(event);" ></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:CommandField ShowDeleteButton="True" />

    </Columns>

    </asp:GridView>





    ********************************************************************************************

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    try
    {
    FirstWorkStageGVRow();
    }
    catch (Exception EC)
    {
    DBHelper.LogError(EC.Message);
    ScriptManager.RegisterStartupScript(this, GetType(), "Show Modal Popup", "showmodalpopup('Alert !! " + EC.Message + "');", true);
    }
    }
    }


    private void FirstWorkStageGVRow()
    {
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("Project_No", typeof(string)));
    dt.Columns.Add(new DataColumn("WorkStage_ID", typeof(string)));
    dt.Columns.Add(new DataColumn("WorkStage_Name", typeof(string)));
    dt.Columns.Add(new DataColumn("WorkStage_Start_Date", typeof(string)));
    dt.Columns.Add(new DataColumn("WorkStage_End_Date", typeof(string)));
    dt.Columns.Add(new DataColumn("WorkStage_Percentage", typeof(string)));
    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dr["Project_No"] = string.Empty;
    dr["WorkStage_ID"] = string.Empty;
    dr["WorkStage_Name"] = string.Empty;
    dr["WorkStage_Start_Date"] = string.Empty;
    dr["WorkStage_End_Date"] = string.Empty;
    dr["WorkStage_Percentage"] = string.Empty;
    dt.Rows.Add(dr);
    ViewState["CurrentTable"] = dt;
    grdWSPlanning.DataSource = dt;
    grdWSPlanning.DataBind();
    }


    protected void grdWSPlanning_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    try
    {
    //DataTable dt = new DataTable();
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    grdWSPlanning_Items_Binding(e.Row);
    }
    }

    catch (Exception EC)
    {
    DBHelper.LogError(EC.Message);
    ScriptManager.RegisterStartupScript(this, GetType(), "Show Modal Popup", "showmodalpopup('Alert !! " + EC.Message + "');", true);
    }
    }

    private void grdWSPlanning_Items_Binding(GridViewRow gvRow)
    {
    //if (ViewState["AddedWorkStages"] == null || ViewState["AddedWorkStages"].ToString() == "")
    //{
    objGen.Type = "WORK_STAGE";
    dtResult = objGen.fn_CommonDetails();
    if (dtResult.Rows.Count > 0)
    {
    DropDownList gvddlWSNo = (DropDownList)gvRow.FindControl("gvddlWSNo");
    gvddlWSNo.DataSource = dtResult;
    gvddlWSNo.DataTextField = "WorkStage";
    gvddlWSNo.DataValueField = "WorkStageId";
    gvddlWSNo.DataBind();
    gvddlWSNo.Items.Insert(0, "Select");
    gvddlWSNo.Items.FindByText("Select").Value = Convert.ToString(0);
    }
    //TextBox gvtxtPRNO = (TextBox)gvRow.FindControl("gvtxtPRNO");
    //gvtxtPRNO.Text = "";
    //}
    //else
    //{

    // DropDownList gvddlWSNo = (DropDownList)gvRow.FindControl("gvddlWSNo");
    // dtResult = (DataTable)ViewState["AddedWorkStages"];
    // gvddlWSNo.DataSource = dtResult;
    // gvddlWSNo.DataTextField = "WorkStage";
    // gvddlWSNo.DataValueField = "WorkStageId";
    // gvddlWSNo.DataBind();
    // gvddlWSNo.Items.Insert(0, "Select");
    // gvddlWSNo.Items.FindByText("Select").Value = Convert.ToString(0);

    //}
    }

    protected void gvButtonAdd_Click(object sender, EventArgs e)
    {
    AddNewRow();
    }

    private void AddNewRow()
    {
    int rowIndex = 0;

    if (ViewState["CurrentTable"] != null)
    {
    if (Validate_Data() == false) // User want to Add new Ws and First or Above Row is not Added with All Filled Values in Grid
    {
    return;
    }
    else // User want to Add new Ws and First or Above Row is Added with All Filled Values in Grid
    {
    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
    DataRow drCurrentRow = null;
    if (dtCurrentTable.Rows.Count > 0)
    {
    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
    {
    TextBox TextBoxProjectNo = (TextBox)grdWSPlanning.Rows[rowIndex].Cells[1].FindControl("gvtxtPRNO");
    DropDownList DrpDownWS = (DropDownList)grdWSPlanning.Rows[rowIndex].Cells[2].FindControl("gvddlWSNo");
    TextBox TextBoxWsSDate = (TextBox)grdWSPlanning.Rows[rowIndex].Cells[3].FindControl("gvtxtWSSDATE");
    TextBox TextBoxWsEDate = (TextBox)grdWSPlanning.Rows[rowIndex].Cells[4].FindControl("gvtxtWSEDATE");
    TextBox TextBoxWsCompletion = (TextBox)grdWSPlanning.Rows[rowIndex].Cells[5].FindControl("gvtxtWSCOPLETION");

    drCurrentRow = dtCurrentTable.NewRow();
    drCurrentRow["RowNumber"] = i + 1;

    dtCurrentTable.Rows[i - 1]["Project_No"] = TextBoxProjectNo.Text.Trim();
    dtCurrentTable.Rows[i - 1]["WorkStage_ID"] = DrpDownWS.SelectedItem.Value.Trim();
    dtCurrentTable.Rows[i - 1]["WorkStage_Name"] = DrpDownWS.SelectedItem.Text.Trim();
    dtCurrentTable.Rows[i - 1]["WorkStage_Start_Date"] = TextBoxWsSDate.Text.Trim();
    dtCurrentTable.Rows[i - 1]["WorkStage_End_Date"] = TextBoxWsEDate.Text.Trim();
    dtCurrentTable.Rows[i - 1]["WorkStage_Percentage"] = TextBoxWsCompletion.Text.Trim();

    rowIndex++;
    }
    dtCurrentTable.Rows.Add(drCurrentRow);
    ViewState["CurrentTable"] = dtCurrentTable;

    grdWSPlanning.DataSource = dtCurrentTable;
    grdWSPlanning.DataBind();
    }
    }
    }
    else
    {
    Response.Write("ViewState is null");
    }
    SetPreviousData();
    }
  • #761510
    Hello Durgesh Singh,

    Please don't post the whole code. Post only that much which is causing an error or problem. So that we can understand your question and code very fast and easily.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761511
    Hi
    Durgesh Singh,

    Your code Some class references so not working.

    So I have made gridview with ddl binding code here try this

    client Side



    <asp:GridView ID="DtgData" runat="server" AutoGenerateColumns="false" OnRowDataBound="DtgData_RowDataBound" >
    <Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" />
    <asp:TemplateField HeaderText="Location">
    <ItemTemplate>
    <asp:DropDownList ID="ddlPlace" runat="server" Width="100px"/>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>


    Server Side

    protected void DtgData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    DataTable location = new DataTable();
    location.Columns.Add("ID");
    location.Columns.Add("Place");
    DataRow dr = location.NewRow();
    dr["ID"] = 1;
    dr["Place"] = "India";
    location.Rows.Add(dr);
    dr = location.NewRow();
    dr["ID"] = 2;
    dr["Place"] = "Srilanka";
    location.Rows.Add(dr);
    var ddl = (DropDownList)e.Row.FindControl("ddlPlace");
    ddl.DataSource = location;
    ddl.DataTextField = "Place";
    ddl.DataValueField = "ID";
    ddl.DataBind();
    }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    DataTable orderDetail = new DataTable();
    orderDetail.Columns.Add("Name");
    DataRow dr = orderDetail.NewRow();
    dr["Name"] = "AA";
    orderDetail.Rows.Add(dr);
    dr = orderDetail.NewRow();
    dr["Name"] = "BB";
    orderDetail.Rows.Add(dr);
    DtgData.DataSource = orderDetail;
    DtgData.DataBind();
    }
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #761514
    Thanks Dotnet Developer-2015, but this is not my query, I m again explaning my requirement and hope you can give better solution for same.

    I hv a Drop Down with Gridview with Item Template Column. Drop Down is binding List from Database (15 records are available). I hv Add Button In Grid Footer to add More Row in Grid. My Requirement is,

    When Any new Row is Added by Add Button In Grid Footer, Then In Drop Down list, Previous all rows Item of Drop Down, should not be Bind or if Bind, then User Can'ot select that Item in Drop Down List. I m googling but not find yet

    For Example...if we have aaa,bbb,ccc,ddd in a dropdown inside a gridview and we have selected aaa in row1 of the gridview.. Then only bbb,ccc,ddd should display in the dropdown of the other rows..

  • #761515
    Hello Durgesh Singh,

    You means you want empty Drop Down or items which are also used should not be bind again??

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments