如何将列添加到现有数据表中? [英] How can i add a column to an existing datatable?

查看:105
本文介绍了如何将列添加到现有数据表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在填充数据表.我需要添加另一列
加载并显示到现有数据表时显示
它在数据列表中.该列将是图片的链接.
我不想将指向图片的链接存储在数据库中,但是会将路径与字段连接起来
从select语句中组成一个链接.
如何将列添加到现有数据表中.


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;


谢谢

推荐答案

好,我真的看不到您遇到的问题,因为您似乎完全按照您的要求做了..所以万一您没有意识到它,检查我在您自己的代码中提出的注释,以了解您在何处进行添加
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
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


这篇关于如何将列添加到现有数据表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆