1

我是 ASP.NET 的初学者并正在尝试学习 windows 窗体,我正在尝试在 windows 窗体上创建一个简单的学生表,我将能够在学生表中添加、更新和删除其详细信息。我想使用存储过程进行删除和触发器进行更新。我已经完成了添加部分,它工作正常。我对实现触发器没有太多想法。现在我正在尝试使用 CellEndEdit 属性进行更新和删除,因为当我在网上搜索时,它似乎是最快的编辑和删除方式。我现在得到一个参数超出范围异常。你能帮忙解决它吗,如果可能的话,还给我一个实现触发器的想法。我已经附上了我的表单视图和数据库结构以及到目前为止我已经完成的代码。任何帮助,将不胜感激。

Class1.cs 的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public class Class1
    {
    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();

    public Class1()
    {
        con.ConnectionString = "server=.\\sqlexpress;database=sample;trusted_connection=true";
        cmd.Connection = con;
    }

    public void opencon()
    {
        if (con.State == ConnectionState.Open)
            con.Close();
        con.Open();
    }

    public SqlConnection getcon
    {
        get
        {
            return con;
        }
    }

    public SqlCommand getcmd
    {
        get
        {
            return cmd;
        }

    }

    public DataSet dataview()
    {
        //cmd.CommandType = CommandType.Text;
        //cmd.CommandText = "select * from Student_table";
        SqlDataAdapter adp = new SqlDataAdapter("select * from Student_table",con);
        DataSet ds = new DataSet();
        //adp.SelectCommand = cmd;
        adp.Fill(ds, "Student_table");
        return ds;
    }
}
}

Form.cs 的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    Class1 con = new Class1();
    DataGridView grid = new DataGridView();

    public Form1()
    {
        InitializeComponent();
        //grid.CellEndEdit += new DataGridViewCellEventHandler();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.student_tableTableAdapter.Fill(this.sampleDataSet.Student_table);
    }

    private void button4_Click(object sender, EventArgs e)
    {

        con.opencon();
        lblstatus.Text = "Connected";

    }

    private void button1_Click(object sender, EventArgs e)
    {
        con.opencon();
        con.getcmd.CommandText="insert into Student_table values('"+txtstname.Text+"','"+txtgrname.Text+"', '"+Convert.ToDateTime(dob.Value)+"','"+txtadd1.Text+"','"+txtadd2.Text+"')";
        con.getcmd.ExecuteNonQuery();
        //con.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        con.opencon();
        con.getcmd.CommandType = CommandType.Text;
        con.getcmd.CommandText = "select * from Student_table";
        SqlDataAdapter adp = new SqlDataAdapter();
        DataSet ds = new DataSet();
        adp.SelectCommand = con.getcmd;
        adp.Fill(ds,"Student_table");
        ds = con.dataview();

        dataGridView1.DataSource = ds.Tables["Student_table"].DefaultView;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void btndel_Click(object sender, EventArgs e)
    {

    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        string query = string.Format(
        "UPDATE Student_table set {0}='{1}' WHERE id={2}",
        grid.Columns[e.ColumnIndex].Name, grid[e.ColumnIndex, e.RowIndex].Value,
        grid[0, e.RowIndex].Value); //**Argument out of range exception error here**

        try
        {
            con.getcmd.CommandType = CommandType.Text;
            con.getcmd.CommandText = query;
            con.opencon();
            con.getcmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.StackTrace);
        }
    }
}

}

请忽略评论,因为我正在尝试不同的事情来实现我的目标:) http://i50.tinypic.com/2yp0kl2.jpg

http://i47.tinypic.com/33az3lw.jpg

4

0 回答 0