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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview binding appends records when checkbox is deselected or anothe

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
I am in a bit of a bind. I have a small application, 'Dubber Tasks' in which I am using checkboxes to select the records. The checkbox schemes works well with my other applications in the project, except this one. Here are the steps:

1) The dubber initially selects the record of interest from MasterGridGV
2) If there are matches, the matchResultsGV GridView is populated.
3) The dubber can then select the record within the matchResultsGV gridview to dub and select 'Add to Inventory'
4) Based on the copies, the last grid is populated

The problem is step 2. The grid repeats/appends the same records if a new record is chosen.

ie. if the record is
RecID - 1
Title- Apples are great
TimeIn- 12/6/2007

Instead of just showing once, the record will be shown in the grid multiple times:
RecID - 1
Title- Apples are great
TimeIn- 12/6/2007

RecID - 1
Title- Apples are great
TimeIn- 12/6/2007
and then some

here's code of the initial checkbox and the binding of matchedResultsGV

Code:
    protected void recordCheckbx_CheckedChanged(object sender, EventArgs e)
    {
        
        RemoveMatchChecks();
        CheckBox checkbox = (CheckBox)sender;
        checkbox.Checked = true;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;

       inventoryGrid.Visible = false;
       matchlbl.Visible = false;
        processLbl.Visible = false;
        cmplteProcessbtn.Visible = false;
        BindNestedGrid();
    }

    private void RemoveMatchChecks()
    {
        foreach (GridViewRow row in MasterGridGV.Rows)
        {
            CheckBox ch = (CheckBox)row.FindControl("recordCheckbx");
            ch.Checked = false;            
        }               
    }
//binding of matchResultsGV

private void BindNestedGrid()
    {
        matchlbl.Visible = true;
        matchlbl.Text = "<span class='header'>Matched Results for ";

        int musicnum;
        string elementnum;
        int prgnum;
        string user;
        string title;
        string video;

        string abcd = ConfigurationManager.ConnectionStrings["RevisedTapeLibrary"].ConnectionString;
        SqlConnection vconn = new SqlConnection(abcd);             

        SqlCommand vcomm = new SqlCommand("SELECT * FROM  view_FoundItems WHERE programmingID=@tape OR MusicID = @music AND ElementNumber = @ele", vconn);

       DataTable detv = new DataTable();
       try
       {
           //vcomm.Connection.Open();

           foreach (GridViewRow nrow in MasterGridGV.Rows)
           {
               CheckBox aCheck = (CheckBox)nrow.FindControl("recordCheckbx");
               if (aCheck != null && aCheck.Checked)
               {
                   int reqrecord = Convert.ToInt32(MasterGridGV.DataKeys[nrow.RowIndex].Value);

                   if (string.Compare(nrow.Cells[5].Text, "NULL") != 0)
                   {
                       prgnum = Convert.ToInt32(nrow.Cells[5].Text);
                       vcomm.Parameters.AddWithValue("@tape", prgnum);
                   }

                   if (string.Compare(nrow.Cells[7].Text, "NULL") != 0)
                   {
                       musicnum = Convert.ToInt32(nrow.Cells[7].Text);
                       vcomm.Parameters.AddWithValue("@music", musicnum);
                   }

                   if (string.Compare(nrow.Cells[10].Text, "NULL") != 0)
                   {
                       elementnum = Convert.ToString(nrow.Cells[10].Text);
                       vcomm.Parameters.AddWithValue("@ele", elementnum);
                   }

                   if (string.Compare(nrow.Cells[2].Text, "0") != 0)
                   {
                       user = Convert.ToString(nrow.Cells[2].Text);
                       matchlbl.Text += user + "'s request #" + reqrecord + "&nbsp;";
                   }

                   if (string.Compare(nrow.Cells[6].Text, "NULL") != 0)
                   {
                       title = Convert.ToString(nrow.Cells[6].Text);
                       matchlbl.Text += title + "</span>";
                   }
                   if (string.Compare(nrow.Cells[8].Text, "NULL") != 0)
                   {
                       video = Convert.ToString(nrow.Cells[8].Text);
                       matchlbl.Text += video + "</span>";
                   }
                   SqlDataAdapter myadap = new SqlDataAdapter(vcomm);
                   myadap.Fill(detv);

                   matchedResultsGV.DataKeyNames = new string[] { "ItemID" };
                   matchedResultsGV.AutoGenerateColumns = false;

                   BoundField db1 = new BoundField();
 
                   db1.DataField = "ItemID";
                   db1.HeaderText = "Tape#";

                   matchedResultsGV.Columns.Add(db1);
//I do this thru db9

                   matchedResultsGV.DataSource = detv;
                   matchedResultsGV.DataBind();

                   int records = matchedResultsGV.Rows.Count;
                   if (records == 1)
                   {
                       testLbl.Text = "There is " + records + " record that matches the request";
                   }
                   else
                   {
                       testLbl.Text = "There are " + records + " records that match the request";
                   }

                   if (records == 0)
                   {
                       newInventoryBtn.Visible = true;
                       testLbl.Text = "No Matched inventory, but you can add the new media.";
                   }
                   else
                   {
                       newInventoryBtn.Visible = false;
                   }
               }
           }
       }
       catch (Exception myerror)
       {     Response.Write("There were errors " + myerror);    }

//doesn't work       
/*finally
       {       matchedResultsGV.Dispose();

       }*/
            
}

 
I resolved my error which was based on checkboxes and bidning of the child grids. If anyone runs into a similar problem, while using checkboxes, the key is in the binding.

I corrected my recordCheckbx_CheckedChanged Event to the following:

Code:
    protected void recordCheckbx_CheckedChanged(object sender, EventArgs e)
    {
        
        RemoveMatchChecks();
        CheckBox checkbox = (CheckBox)sender;
        checkbox.Checked = true;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;

        matchlbl.Visible = false;
        processLbl.Visible = false;
        cmplteProcessbtn.Visible = false;


        BindNestedGrid();
//I added the binding of the second child grid, which relies on the first child records
        inventoryGrid.DataBind();
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top