my form:
my code:
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.SqlClient;
namespace datagrid
{
public partial class Form1 : Form
{
String conn="Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Backiya Lakshmi\\Documents\\Visual Studio 2008\\listbox\\datagrid\\datagrid\\Database1.mdf;Integrated Security=True;User Instance=True";
private void Form1_Load(object sender, System.EventArgs e)
{
BindGrid();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conn);
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
String str ="insert into student(sid,sname,addr,age) VALUES ('" + dataGridView1.Rows[i].Cells["sid"].Value + "', '" + dataGridView1.Rows[i].Cells["sname"].Value + "'," + dataGridView1.Rows[i].Cells["addr"].Value + ",'" + dataGridView1.Rows[i].Cells["age"].Value + "')";
SqlCommand com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Values Saved");
}
}
private void BindGrid()
{
SqlConnection con = new SqlConnection(conn);
con.Open();
String str = "select * from student";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter Sqldataada = new SqlDataAdapter(com);
DataSet ds = new DataSet();
Sqldataada.Fill(ds, "student");
dataGridView1.DataMember = "student";
dataGridView1.DataSource = ds;
con.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
backiyalakshmi KPosted Jul 6, 2012, 7:30 AM
Thank u so much i got the result it is working yaar realy ur a great of me now:-)
Pravin GhadgePosted Jul 6, 2012, 4:50 AM
try this:
private void BindGrid()
{
SqlConnection con = new SqlConnection(conn);
con.Open();
String str = "select * from student";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter Sqldataada = new SqlDataAdapter(com);
DataSet ds = new DataSet();
Sqldataada.Fill(ds, "student");
dataGridView1.Datasource= ds.Tables[0].Defaultview;
con.Close();
}
Blocked AccountPosted Jul 6, 2012, 4:47 AM
Then check for record in your datatable. Put breakpoint in your code and check data is coming from database or not.
Satyapriya NayakPosted Jul 6, 2012, 4:46 AM
First you have to create a table Employees in the database.
Create table Employees (FirstName varchar(50),LastName varchar(50),Age int,Salary int,Marks int)
Try this...
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 Insertdata_from_datagrid__dbcsharp
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
public Form1()
{
InitializeComponent();
}
private void btn_insert_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
str = @"INSERT INTO Employees(FirstName,LastName,Age,Salary,Marks) VALUES ('" + dataGridView1.Rows[i].Cells["FirstName"].Value + "', '" + dataGridView1.Rows[i].Cells["LastName"].Value + "'," + dataGridView1.Rows[i].Cells["Age"].Value + "," + dataGridView1.Rows[i].Cells["Salary"].Value + "," + dataGridView1.Rows[i].Cells["Marks"].Value + ");";
try
{
using (OleDbConnection con = new OleDbConnection(ConnectionString))
{
using (com = new OleDbCommand(str, con))
{
con.Open();
com.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
label1.Text = "Records inserted successfully";
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable table1 = null;
table1 = new DataTable("Employees");
DataRow row1 = null;
DataColumn FirstName = new DataColumn("FirstName");
table1.Columns.Add(FirstName);
DataColumn Lastname = new DataColumn("LastName");
table1.Columns.Add("LastName");
DataColumn Age = new DataColumn("Age");
table1.Columns.Add("Age");
DataColumn Salary = new DataColumn("Salary");
table1.Columns.Add("Salary");
DataColumn Marks = new DataColumn("Marks");
table1.Columns.Add("Marks");
row1 = table1.NewRow();
row1["FirstName"] = "Amit";
row1["LastName"] = "Sharma";
row1["Age"] = 30;
row1["Salary"] = "20000";
row1["Marks"] = "70";
table1.Rows.Add(row1);
row1 = table1.NewRow();
row1["FirstName"] = "Rahul";
row1["LastName"] = "Dravid";
row1["Age"] = 31;
row1["Salary"] = "300000";
row1["Marks"] = "80";
table1.Rows.Add(row1);
row1 = table1.NewRow();
row1["FirstName"] = "Sachin";
row1["LastName"] = "Tendulkar";
row1["Age"] = 32;
row1["Salary"] = "10000000";
row1["Marks"] = "90";
table1.Rows.Add(row1);
DataSet ds = null;
ds = new DataSet();
ds.Tables.Add(table1);
dataGridView1.DataMember = "Employees";
dataGridView1.DataSource=ds;
}
}
}
Thanks
If this post helps you mark it as answer
backiyalakshmi KPosted Jul 6, 2012, 4:33 AM
but DataBind() is Not exist in the program..
Satyapriya NayakPosted Jul 6, 2012, 4:24 AM
Blocked AccountPosted Jul 6, 2012, 3:53 AM
In your BindGrid method add this
dataGridView1.DataBind();
after this dataGridView1.DataSource = ds;
Thanks