Link to home
Create AccountLog in
Avatar of Sirdots
Sirdots

asked on

How to add a column to an existing datatable with data

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?

Thanks.


private void ItemsGet()
        {
            SqlConnection con;
            SqlDataAdapter dladpt;
            DataSet dldst;
            DataTable mydt;
 
            con = new SqlConnection(@"Data Source=zombie;Initial Catalog=pubs;Integrated Security=True");
            dladpt = new SqlDataAdapter("select * from test", con);
            dldst = new DataSet();
            mydt = new DataTable();
            dladpt.Fill(mydt);
            
            PagedDataSource objpds = new PagedDataSource();
            
            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);
            }
            
            MyDataList.DataSource = objpds;
            MyDataList.DataBind();
            
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Sirdots
Sirdots

ASKER

Thanks