从datatable向datagridview添加行 [英] add rows to datagridview from datatable

查看:197
本文介绍了从datatable向datagridview添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Rollno,StudName列的dgv

和DataTable dtStud,它返回像这样的数据

研究名称Rollno StandardId
1 AAA 1 1
2 BBB 2 1
3 CCC 3 1

我想将此数据添加到dgv(DataGridView)列Rollno,StudName

提前thx.

I Have dgv with Rollno,StudName columns

and DataTable dtStud which returns data like this

Studid Name Rollno StandardId
1 AAA 1 1
2 BBB 2 1
3 CCC 3 1

I want add this data to dgv(DataGridView) columns Rollno,StudName

thx in advance.

推荐答案

尝试一下

try this

<asp:gridview id="dgv" runat="server" xmlns:asp="#unknown">
<columns>
                            <asp:templatefield headertext="Name" itemstyle-width="150"> 
                                <itemtemplate>
                                   <%# DataBinder.Eval(Container.DataItem,"Name") %>
                               </itemtemplate>
                              
                            </asp:templatefield>
<pre lang="xml"><asp:templatefield headertext=""RollNo"" itemstyle-width=""150"">
                                <ItemTemplate>
                                   <%# DataBinder.Eval(Container.DataItem,'RollNo');
                              

                            </asp:TemplateField></asp:templatefield>









在aspx.cs中


在页面加载中









in aspx.cs


in page load

if(!IsPostBack)
{
BindGrid();
}


then after Page load create 

 protected void BindGrid()
{
SqlConnection con=new SqlConnection(ConfigurationManager.appSettings["dbConnection"].ToString());
SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from tablename;



        cmd.Connection = con;
        con.Open();


        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        da.Fill(dt);

        dgv.DataSource = dt;
        dgv.DataBind();

        cmd.Dispose();
        con.Close();

}



投票并接受解决方案
如果这对您有帮助
谢谢



vote and accept solution
If this will help you
thanks


这篇关于从datatable向datagridview添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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