I have a gridView on the form in my windows application and in the database I have a table "student" having fields " c_id,sec_id,sub_id,s_id,s_name". On button click(Add) I am executing the query for saving data to database.
In gridView there are 3 column headers namely- "Roll No", "Student Name", "Marks".
Firstly , I want that when I click on button(Add) ,then "s_id" should display in "RollNo" column , "s_name" should display in "Student Name" column and "Marks column" should be editable in gridview.
Secondly, how can I add marks in Marks column in datagrid during runtime and those marks should save in table named "result" in marks column.
I hope my problem is clear.
Kindly give some solution...........
Sam HobbsPosted Feb 21, 2010, 1:33 AM
Starting with a new forms project, you can create a data source using the data sources window. Then drag the data source to the form; the new form. Then new rows (students) can be added by simply entering them in the new row at the bottom of the DataGridView. You could do most everything you have done except without writing any code, and the new rows logic will also work in the code generated by the data source.
You can create a new project in less than an hour that does the above, just to see how it works.
vinni jainPosted Feb 20, 2010, 12:46 AM
Enteries are saving to database but not displaying in gridview
Amit ChoudharyPosted Feb 19, 2010, 10:20 AM
Do one thing on click of button you just save it to database first and just after rebind your Grid. It'll be your solution what you want.
vinni jainPosted Feb 19, 2010, 7:28 AM
In RollNo column of gridview I want s_id should display and in Student name s_name to display. But your code is not doing so , its just entring textbox values to gridview.But on buttonclick I want to add values to database as well as gridview...............................
Plz help me out.
Just tell me the changes to be done in the code which i have posted.
kiran kumarPosted Feb 19, 2010, 6:59 AM
kiran kumarPosted Feb 19, 2010, 6:54 AM
vinni jainPosted Feb 19, 2010, 6:03 AM
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "")
{
MessageBox.Show("Class Name is required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
try
{
cls_Con.cmdOpen(cmdForm);
cmdForm.CommandText = "insert into student(c_id,sec_id,sub_id,s_id,s_name) values(?,?,?,?,?)";
cmdForm.Parameters.AddWithValue("@c_id", OleDbType.Numeric).Value = this.com_Class.SelectedValue;
cmdForm.Parameters.AddWithValue("@sec_id", OleDbType.Numeric).Value = this.com_Section.SelectedValue;
cmdForm.Parameters.AddWithValue("@sub_id", OleDbType.Numeric).Value = this.com_Subject.SelectedValue;
cmdForm.Parameters.AddWithValue("@s_id", OleDbType.Numeric).Value = this.textBox2.Text;
cmdForm.Parameters.AddWithValue("@s_name", OleDbType.Char).Value = this.textBox1.Text.Trim();
cls_Con.cmdClose(cmdForm);
clsmyFunction.setMessageBox("Data Save Successfuly", 1);
}
catch (Exception exp)
{
clsmyFunction.setCreateError(exp.Message);
}
finally
{
cls_Con.cn.Close();
}
}
list_Data();
this.textBox1.Text = "";
this.textBox2.Text = "";
this.com_Class.Text = "";
this.com_Subject.Text = "";
this.com_Section.Text = "";
this.com_Class.Focus();
}
List_Data() is the function for displaying the values from database. The code for that is:
private void list_Data()
{
try
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\Desktop\SchoolManagementSystem\School\School.mdb");
conn.Open();
string strShow = "insert into grid(s_id,s_name)values(@s_id,@s_name)from student where c_id='" + com_Class.SelectedValue + "' & sec_id='" + com_Section.SelectedValue + "'& sub_id='" + com_Subject.SelectedValue + "'";
OleDbCommand cmdShow = new OleDbCommand(strShow, conn);
OleDbDataReader drShow = cmdShow.ExecuteReader();
while (drShow.Read())
{
//dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = drShow["s_id"].ToString();
dataGridView1.Rows[i].Cells[1].Value = drShow["s_name"].ToString();
//dataGridView1.Rows[i].Cells[2].Value = drShow["Marks"].ToString();
i++;
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Training And Placement... ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
But I am getting nothing............
Kindly provide some solution..............
kiran kumarPosted Feb 19, 2010, 4:46 AM
PLS mark the answer if it helps you
vinni jainPosted Feb 19, 2010, 4:33 AM
But the code which you have given me is a web application , and I am making a window application. I am new ASP.NET .........
Kindly tell how can I use your code for my window application
kiran kumarPosted Feb 19, 2010, 4:19 AM
HERE IS THE 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 griddisplay
{
public partial class Form1 : Form
{
int k = 0;
int i = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow();
this.dataGridView1.Rows.Add(dr);
dataGridView1.Rows[k].Cells[0].Value = textBox1.Text;
dataGridView1.Rows[k].Cells[1].Value = textBox2.Text;
SqlConnection con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=kiran;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("insert into grid(name,id)values(@name,@id)",con);
SqlParameter na = new SqlParameter("@name", SqlDbType.NVarChar, 50);
na.Value = dataGridView1.Rows[k].Cells[0].Value;
cmd.Parameters.Add(na);
SqlParameter id1 = new SqlParameter("@id", SqlDbType.NVarChar, 50);
id1.Value = dataGridView1.Rows[k].Cells[1].Value;
cmd.Parameters.Add(id1);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("" + i + " row inserted succesfully to the datagridrow and database");
i++;
k++;
textBox1.Text = "";
textBox2.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
please MARK THE ANSWER IF IT HELPED YOU
Amit ChoudharyPosted Feb 19, 2010, 4:01 AM
Download the project. this is having the same code you want.
and dont forget to mark as answer if it helps
vinni jainPosted Feb 19, 2010, 2:44 AM
Actually on form there are two textboxes each for s_id(Rollno) and s_name(Student Name). I am entering values in these textboxes and on button click I want that these values save to database and simultaneously appear in the gridview.
For this on button click I have used insert command and after insert command I have made a function datafill() for filling values in gridview. But on running my application data is saving in database but not displaying in gridview .
kiran kumarPosted Feb 19, 2010, 2:19 AM
but in the next line, you told that a click on the same button retrieves the values from database...can you be clear?
i shall help you