Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am populating a datatable. I need to add another column
to the existing datatable when it is loaded and display
it in a datalist. This column will be a link to a picture.
I do not want to store the link to the picture in a database but will concatenate the path with a field
from the select statement to make up a link.
How can I add a column to an existing datatable.


SqlConnection con;
         SqlDataAdapter dladpt;
         DataSet dldst;
         DataTable mydt;

         con = new SqlConnection(ConfigurationSettings.AppSettings["dbstring"]);
         dladpt = new SqlDataAdapter("select part_no,part_name from part", con);
         dldst = new DataSet();
         mydt = new DataTable();
         dladpt.Fill(mydt);

                 DataColumn dcolColumn = new DataColumn("Link", typeof(string));
         mydt.Columns.Add(dcolColumn);

         DataRow drowItem;

         foreach (DataRow row in mydt.Columns)
         {
             drowItem = mydt.NewRow();
             drowItem["Link"] = "secret";
             mydt.Rows.Add(drowItem);
         }
          datagridview1.DataSource=mydt;

Thanks
Posted
Updated 30-Aug-10 5:55am
v2
Comments
Dalek Dave 30-Aug-10 10:35am    
I see it was edited just as I went to edit it! :)
Toli Cuturicu 30-Aug-10 12:15pm    
Reason for my vote of 1
No need to ask what you already know well!
[no name] 27-May-14 12:04pm    
But that is on the other Hand no reason to vote 1! Even if I know a field very well, there are circumstances to ask good questions....

1 solution

ok I dont really see what you're having problems with because you seem to have done exactly what you were asking about..so incase you didnt realize it, check up the comments I've put up on your own code to know where you did the addition
C#
SqlConnection con;
SqlDataAdapter dladpt;
DataSet dldst;
DataTable mydt;
con = new SqlConnection(ConfigurationSettings.AppSettings["dbstring"]);
dladpt = new SqlDataAdapter("select part_no,part_name from part", con);
dldst = new DataSet();
mydt = new DataTable();
dladpt.Fill(mydt);
DataColumn dcolColumn = new DataColumn("Link", typeof(string));
/* You created a DataColumn here which is the first thing to do when you want to add a column to a DataTable */
mydt.Columns.Add(dcolColumn);
/* You then added the created column to your DataTable, so it should be working */
DataRow drowItem;
foreach (DataRow row in mydt.Columns)
{
drowItem = mydt.NewRow();
drowItem["Link"] = "secret";
mydt.Rows.Add(drowItem);
}
datagridview1.DataSource=mydt;

If you meant something else, then you need to clarify it better
 
Share this answer
 
Comments
[no name] 30-Aug-10 11:16am    
Excellent code snippets for that specified requirement.

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