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!

unable to bind datatable to datagrid with loop 2

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
I have a datatable that I created and I want to pass some of the values from a gridview into that datatable. I have a loop for the number of rows to add (based on copies). the loop works fine, but none of the other data is being written to the grid. I am testing now so that I can add more fields from the second gridview. what am I missing here?

Code:
 protected void Addbtn_Click(object sender, EventArgs e)
    {
//one of the records from the previous gridview
       int chooseRec = Convert.ToInt32(nestedGrid.SelectedRow.Cells[2].Text);              
        try
        {    

        DataTable myDataTable = new DataTable("MultipleRecs");
        DataColumn myDataColumn;
        DataRow myrow;

        myDataColumn = new DataColumn();
        myDataColumn.DataType = System.Type.GetType("System.Int32");
        myDataColumn.ColumnName = "Tape";
        myDataColumn.ReadOnly = true;
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = System.Type.GetType("System.String");
        myDataColumn.ColumnName = "Barcode";
        myDataColumn.ReadOnly = true;
        myDataTable.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = System.Type.GetType("System.String");
        myDataColumn.ColumnName = "Format";
        myDataColumn.ReadOnly = true;
        myDataTable.Columns.Add(myDataColumn);

        myrow = myDataTable.NewRow();
        myrow["Tape"]=chooseRec;
        myrow["Barcode"]="Sample";
        myrow["Format"]= "Format Example";
        myDataTable.Rows.Add(myrow);
     
            int copies;
            copies = Convert.ToInt32(MasterGrid.SelectedRow.Cells[3].Text);

            for (int i = 1; i < copies; i++)
            {
                myDataTable.Rows.Add(i);
                
            }
            testGrid.DataMember = "MultipleRecs";
            testGrid.DataSource = myDataTable;            
            testGrid.DataBind();   
        }
        catch (SqlException bigerr)
        {
            reqLabel.Text = "Errors loading data<br>" + bigerr;
        }
    }
 
all your columns are readonly. so you can't write to them. remove the readonly lines from the column definations and you should be good.

also, can use alot of overloaded to decrease the amount of code
Code:
int chooseRec = Convert.ToInt32(nestedGrid.SelectedRow.Cells[2].Text);

DataTable myDataTable = new DataTable("MultipleRecs");
myDataTable.Columns.Add("Tape", typeof(int));
myDataTable.Columns.Add("Barcode", typeof(string));
myDataTable.Columns.Add("Format", typeof(string));

myDataTable.Rows.Add(new object[] { chooseRec, "Sample", Format Sample" });

int copies = Convert.ToInt32(MasterGrid.SelectedRow.Cells[3].Text);
for (int i = 1; i <= copies; i++)
{
   //this doesn't work myDataTable.Rows.Add(i);
   myDataTable.Rows.Add(new object[] { 0, string.Empty, string.Empty });
}
testGrid.DataSource = myDataTable;            
testGrid.DataBind();

your catch statement will never catch anything because a sqlexception will never be thrown.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top