窗体数据修改帮我改出代码错误,并能运行!谢谢!!
private void DataBind(int currentPosition){
if (currentPosition == 0)
{
buttonFirst.Enabled = false;
buttonPrevious.Enabled = false;
buttonNext.Enabled = true;
buttonLast.Enabled = true;
}
else
{
if (currentPosition == comboBoxNumber.Items.Count - 1)
{
buttonFirst.Enabled = true;
buttonPrevious.Enabled = true;
buttonNext.Enabled = false;
buttonLast.Enabled = false;
}
else
{
buttonFirst.Enabled = true;
buttonPrevious.Enabled = true;
buttonNext.Enabled = true;
buttonLast.Enabled = true;
}
}
BindingContext[xsglDataSet, "xsxxb"].Position = currentPosition;
string exePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
string fileName = exePath + xsglDataSet.Tables["xsxxb"].Rows[currentPosition]["相片URL"].ToString ();
FileInfo test = new FileInfo(fileName);
if (test.Exists)
{
Image image1 = Image.FromFile(fileName);
pictureBox1.Image = image1;
}
else
{
pictureBox1.Image = null;
}
}
private void Form1_Load(object sender, EventArgs e)
{
int n=xsxxbTableAdapter.Fill (xsglDataSet.xsxxb );
comboBoxNumber.SelectedIndex=0;
DataBind(0);
buttonModiOrcancel.Text="修改数据";
buttonBrowse.Enabled=false;
buttonUpdate.Enabled=false;
textBoxName.ReadOnly=true;
textBoxSex.ReadOnly=true;
textBoxClass.ReadOnly=true;
textBoxBirthday.ReadOnly=true;
textBoxFace.ReadOnly=true;
textBoxNation.ReadOnly=true;
textBoxPlace.ReadOnly=true;
textBoxState.ReadOnly=true;
// TODO: 这行代码将数据加载到表“xsglDataSet.xsxxb”中。您可以根据需要移动或移除它。
this.xsxxbTableAdapter.Fill(this.xsglDataSet.xsxxb);
}
private void comboBoxNumber_SelectedIndexChanged(object sender, EventArgs e)
{
{
int current = comboBoxNumber.SelectedIndex;
DataBind(current);
}
}
private void buttonFirst_Click(object sender, EventArgs e)
{
comboBoxNumber.SelectedIndex = 0;
DataBind(0);
}
private void buttonPrevious_Click(object sender, EventArgs e)
{
int current = comboBoxNumber.SelectedIndex - 1;
comboBoxNumber.SelectedIndex = current;
DataBind(current);
}
private void buttonNext_Click(object sender, EventArgs e)
{
int current = + 1;
comboBoxNumber.SelectedIndex = current;
DataBind(current);
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图像文件(*.bmp)|*.bmp";
DialogResult dlr = ofd.ShowDialog();
if (dlr == DialogResult.OK)
{
Image image1 = Image.FromFile(ofd.FileName);
textBoxPhotoPath.Text = ofd.FileName;
pictureBox1.Image = image1;
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void buttonModiOrcancel_Click(object sender, EventArgs e)
{
if (buttonModiOrcancel.Text == " 修改数据")
{
buttonModiOrcancel.Text = "取消修改";
textBoxName.ReadOnly = false;
textBoxSex.ReadOnly = false;
textBoxClass.ReadOnly = false;
textBoxBirthday.ReadOnly = false;
textBoxFace.ReadOnly = false;
textBoxNation.ReadOnly = false;
textBoxPlace.ReadOnly = false;
textBoxState.ReadOnly = false;
textBoxName.Focus();
buttonBrowse.Enabled = true;
buttonUpdate.Enabled = true;
panel1.Enabled = false;
}
else
{
int n = comboBoxNumber.SelectedIndex;
xsglDataSet.Clear();
xsxxbTableAdapter.Fill ( this.xsglDataSet .xsxxb );
comboBoxNumber.SelectedIndex = n;
BindingContext[xsglDataSet, "xsxxb"].Position = n;
DataBind(n);
buttonModiOrcancel.Text = " 修改数据";
textBoxName.ReadOnly = true;
textBoxSex.ReadOnly = true;
textBoxClass.ReadOnly = true;
textBoxBirthday.ReadOnly = true;
textBoxFace.ReadOnly = true;
textBoxNation.ReadOnly = true;
textBoxPlace.ReadOnly = true;
textBoxState.ReadOnly = true;
buttonBrowse.Enabled = false;
buttonUpdate.Enabled = false;
panel1.Enabled = true;
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
string number = comboBoxNumber.Text;
string name = textBoxName.Text;
string sex = textBoxSex.Text;
string Class = textBoxClass.Text;
string birthday = textBoxBirthday.Text;
string face = textBoxFace.Text;
string nation = textBoxNation.Text;
string Place = textBoxPlace.Text;
string studystate = textBoxState.Text;
string exepath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
string photopath = textBoxPhotoPath.Text;
int n = exepath.Length;
int m = photopath.Length;
if (m > n)
{
photopath = photopath.Substring(n, m - n);
SqlCommand myUpdateCommand = sqlconnection1.CreateCommand();
= "update xsxxb set 姓名=@ xm,性别=@xb,班级=@bj," + "出生日期=@csrq, 政治面貌=@zzmm,民族@mz,籍贯=@jg,学习状况=@xxzk," + "相片 URL=@txlj where 学号='" + number + "'";
myUpdateCommand.Parameters.Add("@xm", SqlDbType.NChar, 10, "姓名");
myUpdateCommand.Parameters.Add("@xb", SqlDbType.NChar, 10, "性别");
myUpdateCommand.Parameters.Add("@bj", SqlDbType.NChar, 10, "班级");
myUpdateCommand.Parameters.Add("@csrq", SqlDbType.NChar, 10, "出生日期");
myUpdateCommand.Parameters.Add("@zzmm", SqlDbType.NChar, 10, "政治面貌");
myUpdateCommand.Parameters.Add("@mz", SqlDbType.NChar, 10, "民族");
myUpdateCommand.Parameters.Add("@jg", SqlDbType.NChar, 10, "籍贯");
myUpdateCommand.Parameters.Add("@xxzk", SqlDbType.NChar, 10, "学习状况");
myUpdateCommand.Parameters.Add("@txlj", SqlDbType.NChar, 10, "相片URL");
myUpdateCommand.Parameters["@ xm"].Value = name;
myUpdateCommand.Parameters["@ xb"].Value = sex;
myUpdateCommand.Parameters["@ bj"].Value = Class;
myUpdateCommand.Parameters["@ csrq"].Value = birthday;
myUpdateCommand.Parameters["@ zzmm"].Value = face;
myUpdateCommand.Parameters["@ mz"].Value = nation;
myUpdateCommand.Parameters["@ jg"].Value = Place;
myUpdateCommand.Parameters["@ xxzk"].Value = studystate;
myUpdateCommand.Parameters["@ txlj"].Value = photopath;
SqlConnection1.Open();
try
{
myUpdateCommand.ExecuteNonQuery();
MessageBox.Show("数据更新。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception E)
{
MessageBox.Show("数据格式错误,不能更新数据。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
SqlConnection1.close();
n = comboBoxNumber.SelectedIndex;
xsglDataSet.Clear();
xsxxbTableAdapter.Fill(xsglDataSet.xsxxb);
comboBoxNumber.SelectedIndex = n;
BindingContext[xsglDataSet, "xsxxb"].Position = n;
DataBind(n);
buttonModiOrcancel.Text = "修改数据";
textBoxName.ReadOnly = true;
textBoxName.ReadOnly = true;
textBoxSex.ReadOnly = true;
textBoxClass.ReadOnly = true;
textBoxBirthday.ReadOnly = true;
textBoxFace.ReadOnly = true;
textBoxNation.ReadOnly = true;
textBoxPlace.ReadOnly = true;
textBoxState.ReadOnly = true;
buttonBrowse.Enabled = false;
buttonUpdate.Enabled = false;
panel1.Enabled = true;
}
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.xsxxbTableAdapter.FillBy(this.xsglDataSet.xsxxb);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}