0

I want to put my data into data grid view, by manually adding columns in it, so how can i do that,please help me.

Thank You in Advance.

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
dinesh
  • 3
  • 2
  • This link can help you: http://stackoverflow.com/questions/5524075/programatically-add-new-column-to-datagridview – RamHS Oct 22 '13 at 11:57

2 Answers2

2

Try the following that might solve your problem:

Ensure that the gridview(assuming he ID="gridview1") is in the markup and that AutoGenerateColumns="True"

DataTable dataTable = new dataTable();
dataTable.Columns.Add("Column1");
dataTable.Columns.Add("Column2");
dataTable.Columns.Add("Column3");

DataRow dataRow = dataTable.NewRow();

dataRow["Column1"] = "";
dataRow["Column2"] = "";
dataRow["Column3"] = "";

dataTable.Rows.Add(dataRow);

gridview1.DataSource = datatable;
gridview1.DataBind();
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
0

What about something like:

var grd = new GridView();
grd.AutoGenerateColumns = false;
BoundField field = new BoundField();
field.DataField = "CustomerName";
field.HeaderText = Resources.GlobalResources.Customer;
DataControlField col = field;
grd.Columns.Add(col);
grd.DataSource = sortedCustomers;
grd.DataBind();
Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51