how to bind Datagridview in windows application using c#.net

Posted by Sudheep.grandhe under C# on 10/13/2011 | Points: 10 | Views : 64459 | Status : [Member] | Replies : 6
Hi,

how to bind Datagridview in windows application using c#.net step by step procedure.please do the needful.

Best,
Sudheep.



Responses

Posted by: Syedshakeer on: 10/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,
Check the below links
1)http://www.aspdotnet-suresh.com/2010/12/v-behaviorurldefaultvmlo_27.html

2)http://www.codeproject.com/KB/books/PresentDataDataGridView.aspx

Syed Shakeer Hussain

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.biswas on: 10/13/2011 [Member] Platinum | Points: 25

Up
0
Down
Datagridview.DataSource = < THE DATA SOURCE>


Below is an example where we are binding a datatable to drigview

DataTable dt = new DataTable();

dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("Salary");

for (int i = 1; i <= 10; i++)
dt.Rows.Add(
string.Concat("Name", i)
, i + 10
, i * 2000
);

dgView.DataSource = dt;


Best Regards,
Niladri Biswas

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vivekanand on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi!!
as per your requirement I am attaching one sample project .
If you have any Query let me know.

Thanks & Regards
Vivekanand Singh
 Download source file

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay13mar on: 10/14/2012 [Member] Starter | Points: 25

Up
0
Down
Check the link . This is a nice and simple article for binding grid view
http://dotnetpools.com/Article/ArticleDetiail/?articleId=38&title=How%20To%20Bind%20GridView%20In%20Windows%20Application%20Using%20C#

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\EMP.mdb" />
</appSettings>
</configuration>


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Insert_Update_Delete_DataGridView
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
bindgrid();
}
private void bindgrid()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from employee";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataMember="employee";
dataGridView1.DataSource = ds;
con.Close();
}
private void Clear()
{
txt_sno.Text = "";
txt_name.Text = "";
txt_address.Text = "";
txt_age.Text = "";
}

private void btn_edit_Click(object sender, EventArgs e)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
txt_sno.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
txt_name.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
txt_address.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
txt_age.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
}

private void btn_update_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "Update employee set sno=@sno,name=@name,address=@address,age=@age Where sno=@sno";
com = new OleDbCommand(str, con);
com.Parameters.AddWithValue("@sno", txt_sno.Text);
com.Parameters.AddWithValue("@name", txt_name.Text);
com.Parameters.AddWithValue("@address", txt_address.Text);
com.Parameters.AddWithValue("@age", txt_age.Text);
com.ExecuteNonQuery();
MessageBox.Show("Record Successfully Updated");
con.Close();
bindgrid();
Clear();
}

private void btn_delete_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
com = new OleDbCommand();
if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
{
com.CommandText = "DELETE FROM employee WHERE ID=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
con.Open();
com.Connection = con;
com.ExecuteNonQuery();
con.Close();
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
MessageBox.Show("Record Successfully Deleted");
}
bindgrid();
}

private void btn_insert_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "Insert into employee(sno,name,address,age) Values (@sno,@name,@address,@age)";
com = new OleDbCommand(str, con);
com.Parameters.AddWithValue("@sno", txt_sno.Text);
com.Parameters.AddWithValue("@name", txt_name.Text);
com.Parameters.AddWithValue("@address", txt_address.Text);
com.Parameters.AddWithValue("@age", txt_age.Text);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Successfully Inserted");
bindgrid();
Clear();
}
}
}


If this post helps you mark it as answer
Thanks

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response