using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 图书馆管理系统
{
public partial class readerclassify : Form
{
private CurrencyManager cmorders;//数据导航
public readerclassify()
{
InitializeComponent();
}
private void dataGrid1_Navigate(object sender, NavigateEventArgs ne)
{
}
private void readerclassify_Load(object sender, EventArgs e)
{
da1.SelectCommand.Parameters[0].Value = "%%";
da1.Fill(dataSet11);
cmorders = (CurrencyManager)BindingContext[dataSet11, "读者类型"];
}
private void dataGrid1_Navigate_1(object sender, NavigateEventArgs ne)
{
}
private void da1_RowUpdated(object sender, System.Data.SqlClient.SqlRowUpdatedEventArgs e)
{
}
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
//cmorders = (CurrencyManager)BindingContext[dataSet11, "读者类型"];
if (e.Button.ToolTipText == "首记录")
{
this.dataGrid1.UnSelect(cmorders.Position); //取消原选中的行
cmorders.Position = 0;
this.dataGrid1.Select(cmorders.Position); //选中当前行
this.dataGrid1.CurrentRowIndex = cmorders.Position; //移动表头指示图标
return;
}
if (e.Button.ToolTipText == "上一记录")
{
if (cmorders.Position >= 0)
{
this.dataGrid1.UnSelect(cmorders.Position);
cmorders.Position--;
this.dataGrid1.Select(cmorders.Position);
this.dataGrid1.CurrentRowIndex = cmorders.Position;
}
return;
}
if (e.Button.ToolTipText == "下一记录")
{
if (cmorders.Position <= cmorders.Count - 1)
{
this.dataGrid1.UnSelect(cmorders.Position);
cmorders.Position++;
this.dataGrid1.Select(cmorders.Position);
this.dataGrid1.CurrentRowIndex = cmorders.Position;
}
return;
}
if (e.Button.ToolTipText == "尾记录")
{
this.dataGrid1.UnSelect(cmorders.Position);
cmorders.Position = cmorders.Count - 1;
this.dataGrid1.Select(cmorders.Position);
this.dataGrid1.CurrentRowIndex = cmorders.Position;
return;
}
if (e.Button.ToolTipText == "新增")
{
cmorders.AddNew();
//设置默认值
txt3.Text = "1";
txt4.Text = "1";
txt5.Text = "1";
SetModifyMode(true);
}
if (e.Button.ToolTipText == "修改")
{
SetModifyMode(true);
}
if (e.Button.ToolTipText == "删除")
{
DialogResult result = MessageBox.Show("确认删除?", "删除数据", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
if (cmorders.Count > 0)//立即从数据集中删除
{
cmorders.RemoveAt(cmorders.Position);
da1.Update(dataSet11);
}
else
MessageBox.Show("表中为空,已无可删除数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (e.Button.ToolTipText == "提交")
{
if (txt2.Text.Trim() == "")//检查非空字段
{
MessageBox.Show("类型名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
cmorders.EndCurrentEdit();
if (dataSet11.GetChanges() != null)
{
try
{
da1.Update(dataSet11);
SetModifyMode(false);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
dataSet11.RejectChanges();
}
}
return;
}
if (e.Button.ToolTipText == "取消")
{
try
{
cmorders.CancelCurrentEdit(); //取消编辑
SetModifyMode(false);
}
catch (Exception express)
{
MessageBox.Show(express.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
if (e.Button.ToolTipText == "退出")
{ this.Close(); }
}
private void SetModifyMode(bool blnEdit)
{
//设置文本框和下拉列表框属性
txt2.ReadOnly = !blnEdit;
txt3.ReadOnly = !blnEdit;
txt4.ReadOnly = !blnEdit;
txt5.ReadOnly = !blnEdit;
//设置搜索按钮属性
button1.Enabled = !blnEdit;
}
private void button1_Click(object sender, EventArgs e)
{
da1.SelectCommand.Parameters[0].Value = "%%";
if (txt1.Text.Trim() != "")
{
da1.SelectCommand.Parameters[0].Value = "%" + txt1.Text.Trim() + "%";
}
//清空数据表,并根据新设置的查询参数重新填充
dataSet11.读者类型.Clear();
da1.Fill(dataSet11); da1.SelectCommand.Parameters[0].Value = "%%";
if (txt1.Text.Trim() != "")
{
da1.SelectCommand.Parameters[0].Value = "%" + txt1.Text.Trim() + "%";
}
//清空数据表,并根据新设置的查询参数重新填充
dataSet11.读者类型.Clear();
da1.Fill(dataSet11);
}
}
}
下滑线部分出现错误
此 SqlParameterCollection 的 Count=0 的索引 0 无效。
这是什么原因啊?