|
楼主 |
发表于 2014-5-17 14:07:02
|
显示全部楼层
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.SqlTypes;
using System.Data.SqlClient;
namespace studentmanagement
{
public partial class informanage : Form
{
SqlDataAdapter adapter;
DataTable selectedTable;
public informanage()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
teacherform teacherform = new teacherform();
teacherform.Show();
}
private void informanage_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“sinfodatebaseDataSet.sinfor”中。您可以根据需要移动或删除它。
this.sinforTableAdapter.Fill(this.sinfodatebaseDataSet.sinfor);
// BindingSource sinforBindingSource = new BindingSource();
// sinforBindingSource.DataSource = sinfodatebaseDataSet.sinfor;
bindingNavigator1.BindingSource = bindingSource1;
for (int i = 0; i < sinfodatebaseDataSet7.Tables.Count; i++)
{
listBox1.Items.Add(sinfodatebaseDataSet7.Tables[i].TableName);
}
listBox1.SelectedIndex = 0;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
;
//sinfodatebaseDataSet1TableAdapters.sinforTableAdapter adapter = new sinfodatebaseDataSetTableAdapters.sinforTableAdapter();
//adapter.Fill(this.sinfodatebaseDataSet.sinfor);
}
private void button3_Click(object sender, EventArgs e)
{
try
{
bindingSource1.AddNew();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("请单击选择要删除的行");
}
else
{
if (MessageBox.Show("确定要删除这行吗", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
{
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[i]);
}
}
}
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
selectedTable = sinfodatebaseDataSet7.Tables[index];
string queryString = "select * from " + selectedTable.TableName;
adapter = new SqlDataAdapter(queryString, Properties.Settings.Default.sinfodatebaseConnectionString);
SqlCommandBuilder builer = new SqlCommandBuilder(adapter);
adapter.InsertCommand = builer.GetInsertCommand();
adapter.DeleteCommand = builer.GetDeleteCommand();
adapter.UpdateCommand = builer.GetUpdateCommand();
adapter.Fill(selectedTable);
bindingSource1.DataSource = selectedTable;
dataGridView1.DataSource = bindingSource1;
}
private void button2_Click(object sender, EventArgs e)
{
try
{
this.Validate();
bindingSource1.EndEdit();
adapter.Update(sinfodatebaseDataSet7.Tables[listBox1.SelectedIndex]);
MessageBox.Show("保存成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "保存失败");
}
}
|
|