DataTable table;
private void showdata1()
{
string file_name = "d:\\excel.xls";
string strConn = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from [Sheet2$]", conn);
table = new DataTable();
adp.Fill(table);
dataGridView4.DataSource = table;
conn.Close();
}
private void btn添加_Click(object sender, EventArgs e)
{
string file_name = "d:\\excel.xls";
//拼装一个连接字符串
string strConn = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0";
//声明一个连接对象
//声明一个连接对象
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
if (MessageBox.Show("你确定要修改本条记录吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
string update = "update [Sheet2$] set title='" + textBox1.Text.ToString ().Trim ()+ "',teacher='" + textBox2.Text.ToString ().Trim () + "'";
OleDbCommand mycmd = new OleDbCommand(update, conn);
mycmd.ExecuteNonQuery(); //string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0";
//OleDbConnection conn = new OleDbConnection(strConn);
//conn.Open();
//声明一个DataSet
//DataSet ds = new DataSet();
//将查询出的记录集从数据适配器中放到DataSet中
showdata1();
dataGridView4.Refresh();
conn.Close();
}
} private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = Convert .ToString (table.Rows[e.RowIndex][1]);
textBox2.Text = Convert .ToString (table.Rows[e.RowIndex][2]);
}
用Excel作为数据库的,但运行时 textBox1.Text = Convert .ToString (table.Rows[e.RowIndex][1]);
出错,出现未将对象引用设置到对象的实例的错误,不知咋样修改?
private void showdata1()
{
string file_name = "d:\\excel.xls";
string strConn = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter("select * from [Sheet2$]", conn);
table = new DataTable();
adp.Fill(table);
dataGridView4.DataSource = table;
conn.Close();
}
private void btn添加_Click(object sender, EventArgs e)
{
string file_name = "d:\\excel.xls";
//拼装一个连接字符串
string strConn = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0";
//声明一个连接对象
//声明一个连接对象
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
if (MessageBox.Show("你确定要修改本条记录吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
string update = "update [Sheet2$] set title='" + textBox1.Text.ToString ().Trim ()+ "',teacher='" + textBox2.Text.ToString ().Trim () + "'";
OleDbCommand mycmd = new OleDbCommand(update, conn);
mycmd.ExecuteNonQuery(); //string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + file_name + ";Extended Properties=Excel 8.0";
//OleDbConnection conn = new OleDbConnection(strConn);
//conn.Open();
//声明一个DataSet
//DataSet ds = new DataSet();
//将查询出的记录集从数据适配器中放到DataSet中
showdata1();
dataGridView4.Refresh();
conn.Close();
}
} private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = Convert .ToString (table.Rows[e.RowIndex][1]);
textBox2.Text = Convert .ToString (table.Rows[e.RowIndex][2]);
}
用Excel作为数据库的,但运行时 textBox1.Text = Convert .ToString (table.Rows[e.RowIndex][1]);
出错,出现未将对象引用设置到对象的实例的错误,不知咋样修改?
private void dataGridView4_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(table!=null)
{
textBox1.Text = Convert .ToString (table.Rows[e.RowIndex][1]);
textBox2.Text = Convert .ToString (table.Rows[e.RowIndex][2]);
}
}