Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create loop for itemtemplates to repeat in grid

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
Hello.

I am updating information in a database based on the values of a gridview. I need to allow the user to be able to add multiple bits of information, like barcode and format, based on the contents of the selected record.

So, if record XYZ was SELECTED and 10 COPIES needed to be created, there should be 10 fields for barcode and format in the datagrid, while library item is not looped (but is still a field option). I don't know how to go about this and my current try is definitely incorrect. is this even possible or do i need to try looping through a regular form?

here's what I have based on a button_click event which has BindForm() within:
=================================================================
Code:
    private void BindForm()
    {      
        addInventryGV.Visible = true;

        string binddet = ConfigurationManager.ConnectionStrings["RevisedTapeLibrary"].ConnectionString;        
        SqlConnection detconn = new SqlConnection(binddet);
        SqlCommand dubGridDetDS = new SqlCommand("usp_shwApprovOrLibReqDubDet", detconn);
        try
        {
            dubGridDetDS.Connection.Open();
            dubGridDetDS.CommandType = CommandType.StoredProcedure;

            int i;
            for (i = 0; i < addInventryGV.Rows.Count; i++)
            {
                Label reqLabel = new Label();
                reqLabel = (Label)addInventryGV.Rows[0].Cells[1].FindControl("tpeID");


                if (dubberGrid.SelectedRow.Cells[3].Text.ToString() != "NULL")
                {
                    int shwid = Convert.ToInt32(dubberGrid.SelectedRow.Cells[3].Text.ToString());
                    dubGridDetDS.Parameters.Add("@tapeID", SqlDbType.Int);
                    dubGridDetDS.Parameters["@tapeID"].Value = shwid;
                    reqLabel.Text = dubberGrid.SelectedRow.Cells[3].Text.ToString();

                }
                else if (dubberGrid.SelectedRow.Cells[5].Text.ToString() != "NULL")
                {
                    int vidid = Convert.ToInt32(dubberGrid.SelectedRow.Cells[5].Text.ToString());
                    dubGridDetDS.Parameters.Add("@tapeID", SqlDbType.Int);
                    dubGridDetDS.Parameters["@tapeID"].Value = vidid;
                    reqLabel.Text = dubberGrid.SelectedRow.Cells[5].Text.ToString();
                }
                else
                {
                    int eleid = Convert.ToInt32(dubberGrid.SelectedRow.Cells[9].Text.ToString());
                    dubGridDetDS.Parameters.Add("@tapeID", SqlDbType.Int);
                    dubGridDetDS.Parameters["@tapeID"].Value = eleid;
                    reqLabel.Text = dubberGrid.SelectedRow.Cells[9].Text.ToString();
                }
                dubGridDetDS.ExecuteNonQuery();                
                dubGridDetDS.Connection.Close();
                addInventryGV.DataBind();
            }
        }
        catch (SqlException ymu)
        {
            errorLbl.Text= "Errors<br>" + ymu;
        }
    }
 
So for each row in the GridView, you want to create N-number of inputs based on some criteria?

The best way to do this to avoid having to recreate all the controls is to have a Repeater within the GridView rows. The Repeater's ItemTemplate will contain the template for your inputs.

What you'd do is:

1. Bind your GridView with whatever data.
2. In the ItemDataBound event of the GridView, get the collection of elements you want to bind to the Repeater inputs and bind the collection to the Repeater (each element of the collection can be blank if necessary).
3. On PostBack, loop through the GridView items and use FindControl() to locate the Repeater control.
4. Loop through each item in the Repeater you found to determine the values of the inputs.
5. Save all the data.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
i just tried this.

1) i created a new datagrid (which the data successfully posts too).

2) I created an itemtemplate within the gridview.

3) I pulled over an itemtemplate.

this is where it goes downhill.


copies is the field that determines how many times the repeater should repeat. the repeater should contain options for the barcode and format to repeat per the number of copies the user requested. how do I place that successfully within the code? or do I need to create a function to accomplish this for the repeater???

I have this, which produces nothing:

<asp:TemplateField HeaderText="New Controls">
<ItemTemplate>
<asp:Repeater ID="newcopies" runat="server">
<ItemTemplate>
<%# Eval("Copies")%>
<asp:TextBox ID="barcodetxt"runat="server"></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>

thanks in advance for your help.

 
In the RowDataBound event of the GridView, you need to create and bind each Repeater's DataSource. It would look something like this:

Code:
//in RowDataBound

if(e.Row.RowType == DataControlRowType.DataRow)
{
   //"BarCode" is a class you define elsewhere
   List<BarCode> barCodes = new List<BarCode>();

   //add items to the barCodes list
   barCodes.Add( new BarCode(/*parameters*/) );

   Repeater newCopies = (Repeater)e.Row.FindControl("newcopies");
   newCopies.DataSource = barCodes;
   newCopies.DataBind();
}

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top