Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a GridView, and the header field of the GridView are the items from a ListBox in my program. Therefore the number of columns generated is dynamic every time I run. So based on this if I click on Edit button associated inside my GridView, how do I generate TextBox for each cell of that row.

The .cs code I have written is:

C#
protected void DONE4_Click(object sender, EventArgs e)
    {
        Panel7.Visible = true;

        SqlDataAdapter mydat = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
        DataSet dst = new DataSet();
        mydat.Fill(dst, "Table");
        ListBox3.Items.Clear();
        ListBox3.DataSource = dst.Tables[0];
        ListBox3.DataTextField = dst.Tables[0].Columns["Profile_Instance"].ColumnName;
        ListBox3.DataBind();

        int count = ListBox3.Items.Count;

        DataTable dt = new DataTable();
        DataRow rw = default(DataRow);
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            dt.Columns.Add(ListBox1.Items[i].ToString(),
                                           System.Type.GetType("System.String"));
        }

        for (int j = 0; j < count; j++)
        {
            rw = dt.NewRow();
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                rw[ListBox1.Items[i].ToString()] = " ";
            }
            dt.Rows.Add(rw);
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();


        foreach (GridViewRow grdRow in GridView2.Rows)
        {
            DropDownList bind_dropdownlist = new DropDownList();                                                    // defining the property of the DropDownList as bind_dropdownlist
            bind_dropdownlist = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[0].FindControl("Pro_List"));   // finding the  DropDownList from the gridiew for binding
            SqlDataAdapter mydata = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
            DataSet dset = new DataSet();                                                                           // binding the DropDownList with the dataset ds
            mydata.Fill(dset, "Table");
            bind_dropdownlist.DataSource = dset;
            bind_dropdownlist.DataTextField = "Profile_Instance";                                                   // set the DropDownList's DataTextField as designation which display the designation in the dropdownlist after fetching the data from database
            bind_dropdownlist.DataBind();
            bind_dropdownlist.Items.Insert(0, new ListItem("---Choose Profile---", "-1"));
        }
    }


The design code for the GridView is:

<pre lang="HTML"><asp:Panel ID="Panel7" runat="server">
        <asp:GridView ID="GridView2" runat="server" CellPadding="4"
            style="text-align: center; font-size: small" BackColor="White" 
            BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px">

            <Columns>                
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:DropDownList ID="Pro_List" runat="server">
                        <asp:ListItem>--Select--</asp:ListItem>                          
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:CommandField ShowEditButton="True" />

            </Columns>
        </asp:GridView>
    </asp:Panel>


Can someone please help me on this? I hope the question is clear.
Posted

i think you are using Ado.net connectivity based program
try like this
XML
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional" >
   <ContentTemplate>
       <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false"
        CssClass="Gridview"
        HeaderStyle-BackColor="#61A6F8" HeaderStyle-Font-Bold="true"
        HeaderStyle-ForeColor="White"
        ShowFooter="true" onpageindexchanging="OnPaging"
        PageSize="5" onrowcommand="gvDetails_RowCommand"
           onrowdatabound="gvDetails_RowDataBound"
           onrowcancelingedit="gvDetails_RowCancelingEdit"
           onrowediting="gvDetails_RowEditing">
        <Columns>
        <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="RecNo">
                <ItemTemplate>
                    <asp:Label ID="lblid" style="display:none" runat="server" Text='<%#Eval("Id")%>'/>
                    <asp:Label ID="lblRecNo" runat="server" Text='<%#Eval("RecNo")%>'/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblEditId" style="display:none" runat="server" Text='<%#Eval("Id")%>'></asp:Label>
                <asp:Label ID="lblEditRecNo" width="100px"  runat="server" Text='<%#Eval("RecNo")%>' />
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="TType">
                <ItemTemplate>
                    <asp:Label ID="lblTType" runat="server" Text='<%#Eval("TType")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList width="100px" ID="txtEditTType" runat="server"></asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropftrTType" width="100px" AutoPostBack="true" runat="server">
                    </asp:DropDownList>
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Op">
                <ItemTemplate>
                    <asp:Label ID="lblOp" runat="server" Text='<%#Eval("Op")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditOp" CssClass="textBox" width="100px" Text='<%#Eval("Op")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrOp" CssClass="textBox" Width="100px" onkeydown="return jsDecimals(event);" AutoPostBack="true"  runat="server" />
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Cl">
                <ItemTemplate>
                    <asp:Label ID="lblCl" runat="server" Text='<%#Eval("Cl")%>'/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditCl" CssClass="textBox" width="100px" Text='<%#Eval("Cl")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrCl" CssClass="textBox" BackColor="InactiveCaption"  Width="100px"  onkeydown="return jsDecimals(event);" runat="server" />
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Date">
                <ItemTemplate>
                    <asp:Label ID="lblDate"  runat="server" Text='<%#Eval("Date")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditDate" CssClass="textBox" width="80px" Text='<%#Eval("Date") %>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Total">
                <ItemTemplate>
                    <asp:Label ID="lblTotal"  runat="server" Text='<%#Eval("Total")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditTotal" CssClass="textBox" width="100px" Text='<%#Eval("Total")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrTotal"  BackColor="InactiveCaption" CssClass="textBox" width="100px" runat="server" />
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>

            <asp:TemplateField>
               <EditItemTemplate>
                <asp:ImageButton ID="imgbtnUpdate"  CommandName="DoAgain" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
                    <asp:ImageButton ID="imgbtnCancel" CommandName="Cancel" runat="server" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
                </EditItemTemplate>
                <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit"  runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnDelete"  CommandName="DoItNow"   Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:ImageButton ID="imgbtnAdd" CommandName="Insert" runat="server" Height="30px" ImageUrl="~/Images/AddNewitem.jpg" ToolTip="Add new User" ValidationGroup="validaiton" Width="30px" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
   </ContentTemplate>
   </asp:UpdatePanel>

Code Behind file:

C#
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList dp = (DropDownList)e.Row.FindControl("txtEditTType");
                DataTable dt = load_TicketTypes();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem lt = new ListItem();
                    lt.Text = dt.Rows[i][0].ToString();
                    dp.Items.Add(lt);
                }
                dp.Items.Insert(0, new ListItem("Select", "Select"));
                string d=drv[2].ToString();
                dp.SelectedValue = d;
                //dp.Attributes.Add("onBlur", "Detect('"+dp.ClientID+"');");
            }

        }

regards
sarva
 
Share this answer
 
hiii,
read this article will help you

http://msdn.microsoft.com/en-us/library/ms972948.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900