111,120
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
private BindingSource bs;
private bool loaded = false;
private DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "database=northwind;uid=sa;pwd=;server=.";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Products";
conn.Open();
dt = new DataTable();
bs = new BindingSource();
dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
bs.DataSource = dt;
dataGridView1.DataSource = bs;
}
loaded = true;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (loaded)
{
DataRowView drv = bs.Current as DataRowView;
MessageBox.Show(drv["ProductID"].ToString());
}
}
}
DataRowView drv = categoriesBindingSource.Current as DataRowView;
NorthwindDataSet.CategoriesRow row = drv.Row as NorthwindDataSet.CategoriesRow;
MessageBox.Show(row.CategoryID.ToString());