Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Comments
Code 89 23-Feb-12 1:14am    
Improve your question,give clear explanation about your problem..
Jaganathan Bantheswaran 23-Feb-12 1:22am    
Is it Windows app or Web App ?
Ravi Sargam 23-Feb-12 4:57am    
Windows ..........

1 solution

try this

XML
<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>








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
 
Share this answer
 
v2

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