datatable and autogeneratecolumns question

  • Thread starter Thread starter M
  • Start date Start date
M

M

My problem is that AutogenerateColumns="false" does not work with the datatable I built from a hashtable.
(Please don't ask why I'm pulling data from a database, putting it in a hashtable, then putting it into a datatable.)

Everything works great, including hiding the column on the fly. Problem is, the rest of the datatable data/columns still prints out in the
datagrid via autogeneration. So I have duplicate columns....the ones I designated as bound columns, plus the ones that get autogenerated.

Thanks in advance.


Here is the web page code:

<asp:datagrid id="DataGrid5" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="RepCode" HeaderText="RepCode"></asp:BoundColumn>
</Columns>
</asp:datagrid>





Here is the code behind code:

DataTable myT = new DataTable();
myT= sc.getSecurityInfo(1001, 1010, 1, myDiv, true, false);


for(i=0; i < myT.Columns.Count; i++)
{


BoundColumn col = new BoundColumn();
col.DataField = myT.Columns.ColumnName;
col.HeaderText = myT.Columns.ColumnName;
if (col.DataField == myT.Columns["RepCode"].ColumnName)
{
col.Visible = false;
}

DataGrid5.Columns.Add(col);
DataGrid5.DataSource = myT;
DataGrid5.DataBind();
}


Here is the C# snippet of middle tier code that builds the datatable:

DataSet ds = new DataSet();
DataTable myT;
myT = ds.Tables.Add("Company");
DataRow row = ds.Tables["Company"].NewRow();
//Create the Customers DataTable.

int sizeOfHash = fu.sizeOfHashTable(securedCompanyHash);
object[] aValues = new string [sizeOfHash];
int thisI = 0;
foreach(DictionaryEntry entry in securedCompanyHash)
{

string key = Convert.ToString(entry.Key);

myT.Columns.Add(key, typeof(string));

string val = Convert.ToString(entry.Value);

row[key] = val;

if (val == null || val.Length == 0)
{
}else{

aValues[thisI] = val;
thisI++;
}


}

ds.Tables["Company"].LoadDataRow(aValues, false);

return myT;
 
Back
Top