<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Three2Tango &#187; Windows Controls in Visual studio</title>
	<atom:link href="http://www.three2tango.com/category/techcorner/windows-controls-in-visual-studio/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.three2tango.com</link>
	<description>Points To Ponder : The Latest news from the TechWorld,Automobiles,CellPhones and lots of useful Code Snippets</description>
	<lastBuildDate>Fri, 27 Jan 2012 09:03:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Filling a DataGridView-Windows Controls</title>
		<link>http://www.three2tango.com/techcorner/filling-a-datagridview-windows-controls.html/</link>
		<comments>http://www.three2tango.com/techcorner/filling-a-datagridview-windows-controls.html/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 11:47:41 +0000</pubDate>
		<dc:creator>LG</dc:creator>
				<category><![CDATA[Tech Corner]]></category>
		<category><![CDATA[Windows Controls in Visual studio]]></category>
		<category><![CDATA[datagridview]]></category>
		<category><![CDATA[Dot Net]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[windows controls]]></category>

		<guid isPermaLink="false">http://www.three2tango.com/?p=1245</guid>
		<description><![CDATA[DataGridView
                 Is the windows control equivalent to gridview web control.Both are visual studio controls.Here i am trying to explain how to bind values  to a datagridview from a table.
Pre-Requisites
1.)  Open visual studio and start a  project, and add a windows form.
2.)  From toolbox there is a tab&#8221;DATA&#8221;  from this add a DATAGRIDVIEW control.
3.)  A table is made in a certain structure(MS-SqlServer is used here,Table structure given below).
4.)  A class named &#8220;DBAccess.cs&#8221; is made for database connectivity(Explained below).Read More
             ...]]></description>
			<content:encoded><![CDATA[<p><strong>DataGridView</strong><br />
                 Is the windows control equivalent to gridview web control.Both are visual studio controls.Here i am trying to explain how to bind values  to a datagridview from a table.<br />
<strong>Pre-Requisites</strong><br />
<strong>1.)</strong>  Open visual studio and start a  project, and add a windows form.<br />
<strong>2.) </strong> From toolbox there is a tab&#8221;DATA&#8221;  from this add a DATAGRIDVIEW control.<br />
<strong>3.)  </strong>A table is made in a certain structure(MS-SqlServer is used here,Table structure given below).<br />
<strong>4.)  </strong>A class named &#8220;DBAccess.cs&#8221; is made for database connectivity(Explained below).<a href="http://www.three2tango.com/archives/1230">Read More</a></p>
<p>                  After creating the page and placing a &#8220;DATAGRIDVIEW&#8221; in the page, first thing we must do to bind a table to the DataGridView control is to create a database connection.For making the sqlserver connection easier a user defined class <a href="http://www.three2tango.com/archives/1230">&#8220;DBAccess.cs&#8221;</a> is used in which all the database activities are done.The class is there at the bottom of the page,or you can Download it here.Hope it is understandable.<br />
                    Now in the code section of the form,an object of this class is made.And using this object the methods inside the classes are used. A &#8220;GetTable( )&#8221; will help you execute a select query.This method returns a DataTable  &#8220;dt&#8221; which can is set as the DataSource of the DataGridView.</p>
<p><strong>Form1.cs </strong>(The code behind file of the windows form)<br />
       In the code below <em>WindowsSample1</em> is the name of the project,<em>Form1</em> is the name of the form. </p>
<blockquote><p>using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;</p>
<p>namespace WindowsSample1<br />
{</p>
<p>    public partial class Form1 : Form<br />
    {<br />
        DBAccess dba = new DBAccess();<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
    private void Form1_Load(object sender, EventArgs e)<br />
        {<br />
            dba.GetTable(&#8220;select * from SampleTable&#8221;);<br />
            dgv_SampleDataGridView.DataSource = dba.dt;<br />
        }<br />
    }<br />
}</p></blockquote>
<p><strong>Table Structure</strong></p>
<p>            There is no need to explain the table structure here but am doing it  because we are using the same table to do the edit,update and delete operations in a DataGridView So no confusion arises at that time.</p>
<p><strong>TableName : SampleTable</strong><br />
<strong>Fields:</strong></p>
<p>PKey                     varchar(50)       Primary key of the table<br />
FieldName          varchar(50)      A column<br />
AnotherField     varchar(50)      A column<br />
RowStatus            int                        A column to indicate the status, by default it is 1.</p>
<p><strong>DBAccess.cs</strong></p>
<blockquote><p>using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Data.Sql;<br />
using System.Data.SqlClient;<br />
using System.Data.Common;<br />
 <br />
public class DBAccess<br />
{</p>
<p>    public SqlConnection sqlCon = new SqlConnection();<br />
    public SqlCommand sqlCmd = new SqlCommand();<br />
    public SqlDataAdapter sqlDa = new SqlDataAdapter();<br />
    public DataTable dt;<br />
    public DBAccess()<br />
    {<br />
      sqlCon.ConnectionString = &#8220;server=.;uid=sa;pwd=admin;database=test&#8221;;<br />
    }</p>
<p>private SqlConnection GetConnection()<br />
{<br />
    try<br />
    {<br />
        if (sqlCon.State == ConnectionState.Open)<br />
        { sqlCon.Close(); }<br />
        sqlCon.Open();<br />
    }<br />
    catch (Exception ex)<br />
    { sqlCon.Close(); }<br />
    return sqlCon;<br />
}</p>
<p>public DataTable GetTable(string sql)<br />
{<br />
    try<br />
    {<br />
     sqlCmd.Connection = GetConnection();<br />
     sqlCmd.CommandType = CommandType.Text;<br />
     sqlCmd.CommandText = sql;<br />
     sqlDa.SelectCommand = sqlCmd;<br />
     dt = new DataTable();<br />
     sqlDa.Fill(dt);<br />
     if (sqlCon.State == ConnectionState.Open)<br />
     { sqlCon.Close(); }<br />
    }<br />
    catch (Exception ex)<br />
    {<br />
        if (sqlCon.State == ConnectionState.Open)<br />
        { sqlCon.Close(); }<br />
    }<br />
    return dt;<br />
}</p>
<p>public int ExecuteQuery(string sql)<br />
  {<br />
    int rslt = 0;<br />
    try<br />
    {<br />
        sqlCmd.Connection = GetConnection();<br />
        sqlCmd.CommandType = CommandType.Text;<br />
        sqlCmd.CommandText = sql;<br />
        rslt = sqlCmd.ExecuteNonQuery();<br />
        if (sqlCon.State == ConnectionState.Open)<br />
            { sqlCon.Close(); }<br />
    }<br />
    catch (Exception ex)<br />
    {<br />
        if (sqlCon.State == ConnectionState.Open)<br />
        { sqlCon.Close(); }<br />
    }<br />
    return rslt;<br />
  }<br />
}</p></blockquote>
<p><strong>OutPut Screenshot</strong></p>
<p><strong>
<a href="http://www.three2tango.com/wp-content/gallery/general/screenshot3tt.jpg" title="" class="shutterset_singlepic133" >
	<img class="ngg-singlepic ngg-left" src="http://www.three2tango.com/wp-content/gallery/cache/133_watermark_400x200_screenshot3tt.jpg" alt="screenshot3tt.jpg" title="screenshot3tt.jpg" />
</a>
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.three2tango.com/techcorner/filling-a-datagridview-windows-controls.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

