datatable and autogeneratecolumns question

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top