DataTable dt = new DataTable("test_table"); public void Test_Data_Load(object sender, EventArgs e) { DataTable dt = new DataTable("test_table"); dt.Rows.Add("学号"); dt.Rows.Add("姓名"); dt.Rows.Add("成绩"); dt.Rows.Add("备注"); DataRow dr = dt.NewRow(); dr["column0"] = "000000000";dr["column1"] ="XX"; dr["column2"] = "100";dr["column2"] = "好人"; dt.Rows.Add(dr);
Form1 用于用户登录验证
Form1 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;
namespace DataGridViewTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
DBConnect dbc = new DBConnect();
int s = dbc.UserLogin(username, password);
if (1 == s)
{
MessageBox.Show("Incorrect Username");
}
if (2 == s)
{
MessageBox.Show("Incorrect Password");
}
if(0 == s)
{
Form2 form2 = new Form2(this);
//this.Hide();
form2.ShowDialog();
}
}
}
}
Form2, 用记显示数据, 它只有一个DataGridView控件
Form2 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.Collections;
namespace DataGridViewTest2
{
public partial class Form2 : Form
{
Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void Form2_Load(object sender, EventArgs e)
{
DBConnect dbc = new DBConnect();
ArrayList UserList = new ArrayList();
UserList = dbc.ExportData();
UserList.Reverse();
//DataGridView设置
DataTable DataTable1 = new DataTable();
DataTable1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Sex"), new DataColumn("Age"), new DataColumn("Address") });
foreach (User users in UserList)
{
DataTable1.Rows.Add(users.Id, users.Username, users.Sex==0?"男":"女" , users.Age, users.Address);
}
dataGridView1.DataSource = DataTable1;
}
}
}
BDConnect.cs, 数据库链接, 用于验证和提取数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
namespace DataGridViewTest2
{
class DBConnect
{
string username, password;
public int UserLogin(string username, string password)
{
this.username = username;
this.password = password;
//下面这段文字的作用已经在其它博文中解释!
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using(SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserInfo.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM UserLogin WHERE username='"+username + "'";
using(SqlDataReader reader = cmd.ExecuteReader())
{
if(!reader.Read())
{
return 1;//没有此用户
}
else
{
string dbpassword = reader.GetString(reader.GetOrdinal("password"));
if(dbpassword != password)
{
return 2;//用户密码不对
}
else
{
return 0;
}
}
}
}
}
}
public ArrayList ExportData()
{
ArrayList UserList = new ArrayList();
//下面这段文字的作用已经在其它博文中解释!
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserInfo.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM UserInfo";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
User users = new User();
users.Id = (int)reader[0];
users.Username = (string)reader[1];
users.Sex = (int)reader[2];
users.Age = (int)reader[3];
users.Address = (string)reader[4];
UserList.Add(users);
}
}
}
}
return UserList;
}
}
}
Users.cs, 用于存储数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataGridViewTest2
{
class User
{
private int id, sex, age;
private string username, address;
public int Id
{
get { return id; }
set { id = value; }
}
public int Sex
{
get { return sex; }
set { sex = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Username
{
get { return username; }
set { username = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
}
}
数据库 的两个表
UserLogin
UserInfo
update:
从数据库里提取信息的代码有所更新, 使用 SqlDataAdapter来查询数据,并将数据填充到DataTable里反回给DataGridView里显示.
public DataTable ExportData()
{
DataTable UserTable = new DataTable();
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserInfo.mdf;Integrated Security=True;User Instance=True"))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM UserInfo";
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
SqlDataAdapter.Fill(UserTable)
}
}
}
return UserTable;
}
}
}
怎么绑定啊?
追答是winform么?
追问哥,能给个代码参考下吗?谢谢,不连接数据库的
追答这东西不是在代码里弄的啊,在界面设计里的属性,里面有个Columndefine什么的。点开进去输入就好,给你个教程
http://www.cnblogs.com/top5/archive/2010/06/28/1766545.html
C# 怎么把查询到的数据从datatable用foreach循环加载到datagridview
所以直接将datatable的行转成数组就可以添加到datagridview中了 dataGridView1.Rows.Add(line.ItemArray);}
C# 如何把从数据库中查到的数据显示在datagridview中
其实很简单。我们只需要在窗体上拖放一个DataGridView 然后双击窗体空白处,在代码文件的Form1_Load事件中添加括号里的代码 private void Form1_Load(object sender, EventArgs e){ string strsql="select * from stop where stop_id>5";\/\/查询语句。SqlConnection conn = new SqlConnection("server=...
C# 怎么把数组中的数据添加到datagridview中
如果“mydatatable”存在而“Mydatatable”不存在,则认为该搜索字符串不区分大小写。一个DataSet可以包含数个DataTable对象,它们具有相同的TableName属性值和不同的Namespace属性值。
...添加到另一个DatagridView2中急用要用代码。
DataRowView drv = Row.DataBoundItem as DataRowView; \/\/获取行中所有数据 row = (DataRow)drv.Row; \/\/将dgv行中数据添加到定义行中 dtable.Rows.Add(row.ItemArray); \/\/添加就可以了。} else { continue;\/\/继续找 } } dataGridView2.DataSource = dtable; \/\/最后绑定就行了 看看是不...
C#如何传值显示到dataGridView上
DataTable dt = new DataTable();dt = (DataTable) DataGridView 1.DataSource;int count = DataGridView 1.ColumnCount;\/\/这里DataGridView1换成你的DataGridView 的 id \/\/如果没有ColumnCount的话,请手动赋值 if (count != 0)\/\/手动赋值不需要判断 { \/\/如果不只增加一行 变换为 for循环...
c#中如何让数据显示在dataGridView上
\/\/DataTable aaa = new DataTable();\/\/显示在表中 \/\/aaa.Load(aa); \/\/装载数据 \/\/dataGridView1.DataSource = aaa;or \/\/DataSet ds = new DataSet(); \/\/读出数据后就和数据库断了 耗资源 \/\/SqlDataAdapter adp = new SqlDataAdapter("select * from works", conn);\/\/adp.Fill(ds)...
...再根据条件提取一部分数据 添加到Datagridview中 求高手指点_百度知 ...
很简单,假设你获取的DataSet中第一张表是DataGridView的数据源,那么你只需要在这个DataTable进行数据筛选就可以了。C#中针对DataTable提供了一个select方法,你可以试试
在C#中如何从数据库中取一张表的一部分字段放入dataGridView
string sql="select 字段,字段 from ...";SqlDataAdapter sda = new SqlDataAdapter(sql, conn);\/\/连接对象 DataTable dt = new DataTable();sda.Fill(dt);datagridview1.datasource=dt;
C#中点击button将需要的信息显示在datagridview中
根据条件查数据你应该会吧,把查询结果放入datatable中,用下面方式放入datagridview中 if (db.Rows.Count > 0) \/\/db是一个datatable变量 { for (int IntRow = 0; IntRow < db.Rows.Count; IntRow++){ string strCol_0 = db.Rows[IntRow][0].ToString();string strCol_1 = db....
C#怎样把SQL数据库查询出来的结果在dataGridView的指定行中显示
要用Js 来操作了. 你这样等於重新绑定一次. dataGridView 被编译解析后也就是一个html table,你可以看下如何给table添加一个新行. 该行每列的数据就用ajax 从后来获取.你可以参考下 insertRow(); insertCell();这两个方法.