Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to the world of .NET so please excuse my lenghty descriptions. I need to create an object that is based upon user input. E.G. If the user enters 5, then the object will have 5 rows; each row will have five columns, four text boxes (one with TextMode=MultiLine) and one drop-down list (DDL).

Should I use a data-bound control? If so, which one and how do I dynamically control the number of rows? Or, do I create an object dynamically? (How do I create and make that object visible?) Once the user confirms the data, it must be then saved to a SQL table; I want save the reference id of the selection in the DDL.

Because I haven't master coding yet, I can only provide an example of the objective:

User indicates that there are 15 people to hire. There are 4 groups: Worker, Supervisor, Manager and Director. The distribution is determined by a method, with the DDL and multi-line text box editable. So, there will be 9 workers, 3 supervisors, 2 managers and 1 director. Each group will have the following: Title, Job Description, Total Number, Location (DDL) and Notes (MultiLine Text).

The Location and Notes are editable by the user. The Location will be populated from a SQL Server reference table containing: Office, First Floor, Front Door, Back Door and Rooftop.

I appreciate the assistance.
Posted
Comments
Kuthuparakkal 28-Oct-12 21:40pm    
What have u tried so far?

1 solution

Use following code:-
C#
 <div>
    <asp:Label ID="lblRowReq" Text="Required rows:" runat="server"></asp:Label>
    <asp:TextBox ID="txtRowReq" runat="server"></asp:TextBox>
     <asp:Button ID="btnRowGenrate" runat="server" Text="Generate Row" 
            onclick="btnRowGenrate_Click" />
    </div>
    <div>
    <asp:gridview ID="gvGenrateRow" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField HeaderText="Normal Text1">
            <ItemTemplate>
                <asp:TextBox ID="TextBox0" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Normal Text2">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Normal Text3">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Multiline Text">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="DropdownList">
            <ItemTemplate>
               <asp:DropDownList ID="ddlCategory" runat="server">
               <asp:ListItem Text="A"></asp:ListItem>
               <asp:ListItem Text="D"></asp:ListItem>
               <asp:ListItem Text="B"></asp:ListItem>
               </asp:DropDownList>
               
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
</asp:gridview>
    </div>



and on .cs page use this:
C#
private void SetFirstRow()
      {

          DataTable dt = new DataTable();
          DataRow dr = null;
          dt.Columns.Add(new DataColumn("Column0", typeof(string)));
          dt.Columns.Add(new DataColumn("Column1", typeof(string)));
          dt.Columns.Add(new DataColumn("Column2", typeof(string)));
          dt.Columns.Add(new DataColumn("Column3", typeof(string)));
          dt.Columns.Add(new DataColumn("Column4", typeof(string)));

          dr = dt.NewRow();
          dr["Column0"] = string.Empty;
          dr["Column1"] = string.Empty;
          dr["Column2"] = string.Empty;
          dr["Column3"] = string.Empty;
          dr["Column4"] = string.Empty;

          dt.Rows.Add(dr);

          //Store the DataTable in ViewState
          ViewState["CurrentTable"] = dt;

          gvGenrateRow.DataSource = dt;
          gvGenrateRow.DataBind();
      }

      protected void btnRowGenrate_Click(object sender, EventArgs e)
      {
              SetFirstRow();
              for (int i = 0; i < Convert.ToInt32(txtRowReq.Text)-1; i++)
                  AddNewRowToGrid();
      }

      private void AddNewRowToGrid()
      {

          int rowIndex = 0;
          if (ViewState["CurrentTable"] != null)
          {
              DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
              DataRow drCurrentRow = null;
              if (dtCurrentTable.Rows.Count > 0)
              {
                  for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                  {
                      TextBox box0 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[0].FindControl("TextBox0");
                      TextBox box1 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                      TextBox box2 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                      TextBox box3 = (TextBox)gvGenrateRow.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                      DropDownList ddl4 = (DropDownList)gvGenrateRow.Rows[rowIndex].Cells[4].FindControl("ddlCategory");

                      drCurrentRow = dtCurrentTable.NewRow();
                      drCurrentRow["Column0"] = box0.Text;
                      drCurrentRow["Column1"] = box1.Text;
                      drCurrentRow["Column2"] = box2.Text;
                      drCurrentRow["Column3"] = box3.Text;
                      drCurrentRow["Column4"] = ddl4.Text;
                      rowIndex++;
                  }

                  dtCurrentTable.Rows.Add(drCurrentRow);
                  gvGenrateRow.DataSource = dtCurrentTable;
                  gvGenrateRow.DataBind();
              }
          }
      }

if you try to solve your problem step by step then its more better,so first step is completed now try for next....happy coding:-) jmd.
 
Share this answer
 
v3
Comments
FabeCode 1-Nov-12 11:06am    
I am able to create the grid, however, it is reset on AutoPostBack. How do I retain the values?
giri001 2-Nov-12 0:46am    
Using viewstate concept you can retain values.
FabeCode 2-Nov-12 0:58am    
I have tried but with no success. How/Where do I save the values? Doesn't the following statement save the values in ViewState:
ViewState["CurrentTable"] = dt;


And, how/where do I restore them? In the PageLoad, right? So, I should use the following in that event:
gvGenrateRow.DataSource = ViewState["CurrentTable"];
gvGenrateRow.DataBind();

Thanks.

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