using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DataGridView_15._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = BindingModel().Tables[0];
dataGridView2.DataSource = NotBindingModel();
}
private DataTable NotBindingModel()
{
DataTable dt = new DataTable("testdd");
dt.Columns.Add("NO",Type.GetType("System.Int32"));
dt.Columns.Add("Job_Number",Type.GetType("System.String"));
dt.Columns.Add("EmployeeName",Type.GetType("System.String"));
string[,] EmployeeInfo = { {"102340","沈波"},{"102341","吴伟华"},{"102344","陈代元"},{"102345","郝军军"},{"102347","龚普乐"},{"102349","范季红"}};
for(int n=0;n<EmployeeInfo.Length/2;n++)
{
DataRow dr = dt.NewRow();
dr[0] = n+1;
dr[1] = EmployeeInfo[n, 0];
dr[2] = EmployeeInfo[n, 1];
dt.Rows.Add(dr);
}
return dt;
}
private DataSet BindingModel()
{
DataSet ds = new DataSet();
SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=system;database=E_TestData");
try
{
SqlDataAdapter sda = new SqlDataAdapter("select * from Employee",cnn);
sda.Fill(ds,"testdata");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
cnn.Close();
}
return ds;
}
}
}