.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Easy Web
Imran Ghani

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

binding a table under button click event inside datagridview

Posted By: Jayakumar     Posted Date: March 03, 2010    Points:2   Category :Windows Application
hi

i need to bind 3 column from a table with datagridview(winform, c#).

under a button click event..

how it can be done...

thanx n advance...

Responses
Author: Naresh             
Posted Date: March 03, 2010     Points: 5   

hi Jayakumar,

If ur question is to bind gridview after a button click then , it is very simple.

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection("Server=LocalHost;DataBase=DATABASENAME;Uid=sa;Pwd=sa");
con1.Open();
SqlCommand cmd1 = new SqlCommand("select COL1,COL2,COL3 FROM TABLE1", con1);
DataTable t = new DataTable();
SqlDataAdapter dr = new SqlDataAdapter(cmd1);
dr.Fill(t);
this.DataGridView1.DataSource = t;
this.DataGrid1View1.DataBind();
con1 .Close();
}


You can make the property AutoGenerated Column of Grid View to false.


thanks

Thanks
Naresh
Author: Jayakumar             
Posted Date: March 04, 2010     Points: 5   

hi

thank u for ur reply.

but i am getting an error like this

"childlist for the field "table" cannot be created"...
y like this.

i am using this code in a menu items(file (menu name)--->opensong (menu items))

how can be solved..

regards

Author: Narayanan             
Posted Date: July 13, 2011     Points: 20   

Hi,
using System.Data.SqlClient


SqlConnection con;
SqlCommand Cmd;


con = new SqlConnection("Server=LocalHost;DataBase=DATABASENAME;Uid=sa;Pwd=sa");
con.Open();
cmd = new SqlCommand("select username,Password FROM Tbl_login", con1);
DataTable Dt = new DataTable();
SqlDataAdapter dr = new SqlDataAdapter(cmd);
dr.Fill(Dt);
this.DataGridView1.DataSource = Dt;
this.DataGrid1

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
Author: Ravi Ranjan Kumar             
Posted Date: July 17, 2011     Points: 20   

hey! its a very basic question and simple googling will give you plenty of articles related to binding gridview to Database. whatever try to navigate given MSDN link and you will get everything what you expected.
http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx



Thanks & Regards
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com
Author: Ravi Ranjan Kumar             
Posted Date: July 17, 2011     Points: 20   

hey! Narayanan how do you know OP using the table named tbl_login and username and password is column belongs to OP table.
and I am not getting about last line of your answer which is
this.DataGrid1
what it does make sense in this code.


Thanks & Regards
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend