Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir,
bellow is my code for dynamically creating columns in gridview from data in textbox.but the problem is i can add one column in gridview.
if anyone knows the answer plz help me..................
C#
public void load_dynamic_grid()
      {
          try
          {
               DataTable dt = new DataTable();
               string name = txtnm.Text;
      DataColumn dcol = new DataColumn(name, typeof(System.String));
      dt.Columns.Add(dcol);


          DataRow drow = dt.NewRow();


          drow[name] = "Rows" ;


          dt.Rows.Add(drow);


      foreach (DataColumn col in dt.Columns)
      {
          //Declare the bound field and allocate memory for the bound field.
          BoundField bfield = new BoundField();

          //Initalize the DataField value.
          bfield.DataField = col.ColumnName;

          //Initialize the HeaderText field value.
          bfield.HeaderText = col.ColumnName;

          //Add the newly created bound field to the GridView.
          gvAlumni.Columns.Add(bfield);
      }

      //Initialize the DataSource
      gvAlumni.DataSource = dt;

      //Bind the datatable with the GridView.
      gvAlumni.DataBind();
          }
          catch (Exception ex)
          {
              throw (ex);
          }

      }

      protected void btnSave_Click(object sender, EventArgs e)
      {
          load_dynamic_grid();
          txtnm.Text = "";
      }
Posted

SET AutoGenerateColumns = "false" for gridview.
 
Share this answer
 
Comments
vineethnair 11-Mar-14 8:51am    
yes,iam already set AutoGenerateColumns = "false"
vineethnair 11-Mar-14 8:51am    
how to add more than one columns in datatable at the same time from one textbox
nandakishoreroyal 12-Mar-14 0:43am    
What does textbox value return??
use this

DataTable dts = new DataTable();
//for add colums
dts.Columns.Add("id", typeof(System.String));
dts.Columns.Add("Property", typeof(System.String));
dts.Columns.Add("Values", typeof(System.String));
dts.Columns.Add("Names", typeof(System.String));

//for add rows
DataRow _mk = dts.NewRow();
_mk["id"] = "1";
_mk["Property"] = "xxx";
_mk["Values"] = "yyy";
_mk["Names"] = "zzz";
dts.Rows.Add(_mk);

may b this will help
 
Share this answer
 
Comments
vineethnair 11-Mar-14 8:53am    
i need the columns dynamically from the textbox.not direct
MAYANK GEETE 11-Mar-14 9:00am    
Is the textbox returning the column count?
if yes
then use for loop for add columns...
private DataTable CreateDynamicTable()
{
DataTable dt = new DataTable("mytable");
string colCount= TextBox1.Text;
string rowCount = TextBox2.Text;
for (int i = 0; i < Convert.ToInt32(rowCount); i++)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
}
for (int i = 0; i < Convert.ToInt32(colCount); i++)
{
dt.Columns.Add("Col" + i, typeof(string));
}
return dt;
}


Bind the datatable returned to a grid view
 
Share this answer
 
C#
public static DataTable dt;
        protected void btnSave_Click(object sender, EventArgs e)
        {
            dt.Columns.Add(txtnm.Text+"\n"+"grade"+txtgrade.Text);
            dt.Rows.Add("ROWS");
            gvAlumni.DataSource = dt;
            gvAlumni.DataBind();
            txtnm.Text = "";
            txtgrade.Text = "";

        }
 
Share this answer
 

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