Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following code to add rows to the datatable. It will take the number from textbox value and will add that much number of rows to the gridview. Its looping correctly thr for loop but adding only one row to grid.

C#
private void CreateGridViewRows()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            for (int i = 1; i <= int.Parse(txtResources.Text.Trim()); i++)
            {
                
                 
                DataRow drCurrentRow = dt.NewRow();
                dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                dt.Columns.Add(new DataColumn("Col1", typeof(string)));
                dt.Columns.Add(new DataColumn("Col2", typeof(string)));
                dt.Columns.Add(new DataColumn("Col3", typeof(string)));
                dt.Columns.Add(new DataColumn("Col4", typeof(string)));
                dt.Columns.Add(new DataColumn("Col5", typeof(string)));
                dt.Columns.Add(new DataColumn("Col6", typeof(string)));
                dt.Columns.Add(new DataColumn("Col7", typeof(string)));
                dr = dt.NewRow();
                dr["RowNumber"] =1;
                dr["Col1"] = string.Empty;
                dr["Col2"] = string.Empty;
                dr["Col3"] = string.Empty;
                dr["Col4"] = string.Empty;
                dr["Col5"] = string.Empty;
                dr["Col6"] = string.Empty;
                dr["Col7"] = string.Empty;
                dt.Rows.Add(dr);
                
            }




Any suggestions pleas eto code changes.Thanks in advance.
Posted

Hi,
This is not right way to create data table
you shod write add column out side loop

try this it will work

C#
private void CreateGridViewRows()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("Col1", typeof(string)));
            dt.Columns.Add(new DataColumn("Col2", typeof(string)));
            dt.Columns.Add(new DataColumn("Col3", typeof(string)));
            dt.Columns.Add(new DataColumn("Col4", typeof(string)));
            dt.Columns.Add(new DataColumn("Col5", typeof(string)));
            dt.Columns.Add(new DataColumn("Col6", typeof(string)));
            dt.Columns.Add(new DataColumn("Col7", typeof(string)));
            for (int i = 1; i <= (3); i++)
            {


                DataRow drCurrentRow = dt.NewRow();
               
                dr = dt.NewRow();
                dr["RowNumber"] = 1;
                dr["Col1"] = string.Empty;
                dr["Col2"] = string.Empty;
                dr["Col3"] = string.Empty;
                dr["Col4"] = string.Empty;
                dr["Col5"] = string.Empty;
                dr["Col6"] = string.Empty;
                dr["Col7"] = string.Empty;
                dt.Rows.Add(dr);

            }
 
Share this answer
 
Comments
Telstra 12-Jan-16 3:06am    
Thanks for help. I got your point.
Dj@y 12-Jan-16 3:09am    
best luck!
try below
C#
private void CreateGridViewRows()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("Col1", typeof(string)));
            dt.Columns.Add(new DataColumn("Col2", typeof(string)));
            dt.Columns.Add(new DataColumn("Col3", typeof(string)));
            dt.Columns.Add(new DataColumn("Col4", typeof(string)));
            dt.Columns.Add(new DataColumn("Col5", typeof(string)));
            dt.Columns.Add(new DataColumn("Col6", typeof(string)));
            dt.Columns.Add(new DataColumn("Col7", typeof(string)));
            for (int i = 1; i <= int.Parse(txtResources.Text.Trim()); i++)
            {
                DataRow dr = dt.NewRow();
                dr["RowNumber"] =i;
                dr["Col1"] = string.Empty;
                dr["Col2"] = string.Empty;
                dr["Col3"] = string.Empty;
                dr["Col4"] = string.Empty;
                dr["Col5"] = string.Empty;
                dr["Col6"] = string.Empty;
                dr["Col7"] = string.Empty;
                dt.Rows.Add(dr);
                
            }
 
Share this answer
 
Comments
Telstra 12-Jan-16 2:41am    
its working.. Thanks for help.

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