当我运行程序时网格不显示数据 [英] Grid does not show data when i run the program
本文介绍了当我运行程序时网格不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我绑定了网格,但是当我运行程序时,数据库中的数据没有显示.请检查下面的代码!
I bind the grid but when I run the program, the data in the database is not showing. Please check the code below!
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 Company
{
public partial class Form1 : Form
{
public string conStr = "Data Source=MINDWORK2;Initial Catalog=myworld;Integrated Security=True";
public Form1()
{
InitializeComponent();
BindCompany();
}
public void BindCompany()
{
SqlConnection con;
SqlDataAdapter da;
SqlCommandBuilder cmd;
//DataRow row;
DataSet ds = new DataSet();
BindingSource bsourse = new BindingSource();
con = new SqlConnection(conStr);
da = new SqlDataAdapter("select * from company", con);
cmd = new SqlCommandBuilder(da);
da.Fill(ds, "company");
dg.DataSource = ds.Tables["company"];
dg.DataSource = bsourse;
}
public int GetNextId(string tableName)
{
//Here code for getting next id from database.
int id = 0;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
string query = "SELECT MAX(ISNULL(id,0))+1 AS ID FROM COMPANY";
SqlCommand command = new SqlCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection = conn;
try
{
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
id = int.Parse(reader["ID"].ToString());
}
}
catch (Exception exc)
{
MessageBox.Show("Exception getting next id: " + exc.Message);
}
finally
{
conn.Close();
conn = null;
}
return id;
}
public bool InsertCompany(int id)
{
bool result = false;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
string query = "INSERT INTO COMPANY(ID,NAME,LOGIN,PASSWORD) VALUES(@ID, @NAME, @LOGIN, @PASSWORD)";
SqlCommand command = new SqlCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection = conn;
command.Parameters.Add("@ID", SqlDbType.Int).Value = id;
command.Parameters.Add("@NAME", SqlDbType.VarChar).Value = txtName.Text;
command.Parameters.Add("@Login", SqlDbType.VarChar).Value = txtLogin.Text;
command.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text;
try
{
command.ExecuteNonQuery();
result = true;
}
catch (Exception exc)
{
MessageBox.Show("Exception in inserting record: " + exc.Message);
}
finally
{
conn.Close();
conn = null;
}
return result;
}
public bool DeleteCompany(int id)
{
bool result = false;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
string query = "DELETE FROM COMPANY WHERE ID = " + id;
SqlCommand command = new SqlCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Connection = conn;
try
{
command.ExecuteNonQuery();
result = true;
}
catch (Exception exc)
{
MessageBox.Show("Exception in deleting record: " + exc.Message);
}
finally
{
conn.Close();
conn = null;
}
return result;
}
private void btnAdd_Click(object sender, EventArgs e)
{
int id = GetNextId("company");
if (InsertCompany(id))
{
MessageBox.Show("Record inserted");
}
else
{
MessageBox.Show("Unable to insert record");
}
}
private void btnDel_Click(object sender, EventArgs e)
{
int id = 5;
if (DeleteCompany(id))
{
MessageBox.Show("Record deleted successfully");
}
else
{
MessageBox.Show("Unable to delete record");
}
}
private void button1_Click(object sender, EventArgs e)
{
monthCalendar1.Visible = true;
}
}
}
推荐答案
您忘记了数据绑定
dg.DataSource = ds.Tables ["company"];
dg.DataBind();
You forgot to databind
dg.DataSource = ds.Tables["company"];
dg.DataBind();
这篇关于当我运行程序时网格不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文