Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am trying to insert values into gridview but not getting any one tell me

this is my code
C#
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 treeview_grossalary
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

       public Form1()
        {
            InitializeComponent();
        }

       private void Form1_Load(object sender, EventArgs e)
       {
           getdata();
       }
        private void getdata()
        {
            string str = "select * from gridsalry";
            SqlDataAdapter ada = new SqlDataAdapter(str, con);
            DataSet ds = new DataSet();
            ada.Fill(ds, "X");
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            string str = "insert into gridsalry values('" + textBox1.Text + "','" +textBox2.Text+ "','" + textBox3.Text +      "','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+textBox8.Text+"')";
            con.Open();
            cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
Posted
Updated 7-Mar-12 1:06am
v2

Add this line
dataGridView1.DataBind()
after
dataGridView1.DataSource = ds.Tables[0];



Sorry i thought it is web appl'n as i saw gridview written.

Add
getdata()
at the end of
btnsave_Click
 
Share this answer
 
v2
Comments
rockpune 7-Mar-12 6:29am    
not getting sir coming error
nagendrathecoder 7-Mar-12 6:32am    
so sorry, i thought it is a web application.
Just call getdata() at the end of button_click event.
rockpune 7-Mar-12 6:40am    
thank u sir but i get that now i want to do total gross salary how can i do tell me
nagendrathecoder 7-Mar-12 6:43am    
I didn't got you. Whats total gross salary?
there is no DataBind() in winforms
so try this

ada.Fill(ds, "X");
dataGridView1.DataSource = ds.Tables["X"];

or

ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
 
Share this answer
 
Comments
rockpune 7-Mar-12 6:30am    
where i have to write this code on button?
i thinkd you right this


C#
private void btnsave_Click(object sender, EventArgs e)
       {
           string str = "insert into gridsalry values('" + textBox1.Text + "','" +textBox2.Text+ "','" + textBox3.Text +      "','"+textBox4.Text+"','"+textBox5.Text+"','"+textBox6.Text+"','"+textBox7.Text+"','"+textBox8.Text+"')";

          
           con.Open();
           cmd = new SqlCommand(str, con);
           cmd.ExecuteNonQuery();
           con.Close();
           gridsalry.update();
       }
the record will be update in grid
 
Share this answer
 

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