locked
Dynamically Binding DataTable To Asp:GridView RRS feed

  • Question

  • User-410261901 posted

    I am attempting to dynamically generate a DataTable then generate a Asp:Gridview and bind the DataTable to the gridview.  I have the below syntax, but only my field Semantics gets bound.  What is wrong with the below?

    private void DynamicGridGeneration()
    {
    	dtInputData = new DataTable();
    	DataRow dr = null;
    	List<ListItem> items = checkboxtest.Items.Cast<ListItem>().Where(li => li.Selected).ToList();
    	dtInputData.Columns.Add("Semantics", typeof(string));
    	dr = dtInputData.NewRow();
    	dr["Semantics"] = string.Empty;
    	foreach (ListItem item in items)
    	{
    		dtInputData.Columns.Add(item.Text, typeof(string));
    		dr[item.Text] = string.Empty;
    	}
    	//Adding the above blank rows to the DataTable
    	dtInputData.Rows.Add(dr);
    	//Store the DataTable in ViewState
    
    	//Dynamically creating the Gridview
    	GridView Gridview1 = new GridView();
    	Gridview1.HeaderStyle.BackColor = System.Drawing.Color.CornflowerBlue;
    	Gridview1.HeaderStyle.Font.Bold = true;
    	Gridview1.HeaderStyle.ForeColor = System.Drawing.Color.White;
    	Gridview1.CellPadding = 5;
    	Gridview1.AutoGenerateColumns = false;
    
    	//Adding fields and doing bindings
    	BoundField sLevel = new BoundField();
    	sLevel.DataField = "Semantics";
    	sLevel.HeaderText = "Semantics";
    	Gridview1.Columns.Add(sLevel);
    
    	int columnCount = dtInputData.Columns.Count;
    	foreach (DataRow row in dtInputData.Rows)
    	{
    		if (columnCount == 2)
    		{
    			BoundField bfield2 = new BoundField();
    			bfield2.DataField = row[1].ToString();
    			bfield2.HeaderText = row[1].ToString();
    			Gridview1.Columns.Add(bfield2);
    		}
    		if (columnCount == 3)
    		{
    			BoundField bfield3 = new BoundField();
    			bfield3.DataField = row[2].ToString();
    			bfield3.HeaderText = row[2].ToString();
    			Gridview1.Columns.Add(bfield3);
    		}
    		if (columnCount == 4)
    		{
    			BoundField bfield4 = new BoundField();
    			bfield4.DataField = row[3].ToString();
    			bfield4.HeaderText = row[3].ToString();
    			Gridview1.Columns.Add(bfield4);
    		}
    		if (columnCount == 5)
    		{
    			BoundField bfield5 = new BoundField();
    			bfield5.DataField = row[4].ToString();
    			bfield5.HeaderText = row[4].ToString();
    			Gridview1.Columns.Add(bfield5);
    		}
    	}
    
    	//Setting DataSource, Binding And Displaying
    	Gridview1.DataSource = dtInputData;
    	Gridview1.DataBind();
    
    	//Adding the gridview to the panel
    	PlaceHolder1.Controls.Add(Gridview1);
    }

    Friday, January 26, 2018 9:13 PM

Answers

  • User-1716253493 posted

    Try this

    	foreach (ListItem item in items)
    	{
    		dtInputData.Columns.Add(item.Text, typeof(string));
    	}
    	dr = dtInputData.NewRow(); //new row
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 26, 2018 11:18 PM

All replies

  • User753101303 posted

    Hi,

    The first thing I see that you'll have this behavior if the items list is empty. But rather than debugging by reading the code you should first use https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour to see exactly what your code does.

    The last part looks weird. You are looping on rows with duplicated code (except for the column number). Seems you should loop on columns with a single BoundField creation. You could also just start wiht autogenerated columns maybe and improve as needed (so that you can check at each step that your code does what you want).

    Friday, January 26, 2018 9:33 PM
  • User-1716253493 posted

    Try this

    	foreach (ListItem item in items)
    	{
    		dtInputData.Columns.Add(item.Text, typeof(string));
    	}
    	dr = dtInputData.NewRow(); //new row
    
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 26, 2018 11:18 PM