如何从数据库绑定gridview?

时间:2015-09-01 09:33:31

标签: database

using System.Data.SqlClient;
namespace EHR
{
    public partial class imagery : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\wittyse\Desktop\database_EHR\EHR.mdf;Integrated Security=True;Connect Timeout=30");
        SqlCommand cmd;
        DataTable dt2 = new DataTable();
    public imagery()
    {
        InitializeComponent();
    }

    public void readdatagrid1()
    {
        //con.Open();
        SqlDataAdapter dpeo = new SqlDataAdapter("Select Img_ID,PEOPLE_INFO.people_ID,PEOPLE_INFO.Name,Imgs,Result from PEOPLE_INFO,IMAGERY_DIAGNOSTIC,MG_NAM where IMAGERY_DIAGNOSTIC.Img_ID=MG_NAM.Mg_nm_ID and PEOPLE_INFO.people_ID=IMAGERY_DIAGNOSTIC.People_ID", con);
        DataSet dpep = new DataSet();
        dpeo.Fill(dpep, "IMAGERY_DIAGNOSTIC");
        dataGridView1.DataSource = dpep.Tables["IMAGERY_DIAGNOSTIC"];
        con.Close();
    }

    private void imagery_Load(object sender, EventArgs e)
    {
        //dataGridView1.DataSource = null;
        readdatagrid1();

        SqlDataAdapter dz = new SqlDataAdapter("Select * from MG_NAM order by Mg_nm_ID", con);
        DataSet dzo = new DataSet();
        dz.Fill(dzo, "MG_NAM");
        comboBox1.DataSource = dzo.Tables["MG_NAM"];
        comboBox1.DisplayMember = "Imgs";

        SqlDataAdapter da = new SqlDataAdapter("Select Name from PEOPLE_INFO order by people_ID", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "PEOPLE_INFO");
        comboBox2.DataSource = ds.Tables["PEOPLE_INFO"];
        comboBox2.DisplayMember = "Name";
        con.Close();
    }
    public int comck=0;
    public int idx;
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        con.Close();
        string Query = "Select Mg_nm_ID,Imgs from MG_NAM where Imgs='" + comboBox1.Text + "'";
        con.Open();
        SqlCommand createCommand = new SqlCommand(Query, con);
        SqlDataReader dr = createCommand.ExecuteReader();

        while (dr.Read())
        {
            textBox4.Text = dr.GetInt32(0).ToString();
            //textBox1.Text = dr.GetInt32(1).ToString();
            idx = Convert.ToInt32(textBox4.Text);
        }
        dr.Close();
        comck = 1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(id+" index is "+comboBox1.SelectedIndex,"info");
    }

    public int comck2 = 0;
    public int id;
    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        con.Close();
        string Query = "Select people_ID,Name from PEOPLE_INFO where Name='" + comboBox2.Text + "'";
        con.Open();
        SqlCommand createCommand = new SqlCommand(Query, con);
        SqlDataReader dr = createCommand.ExecuteReader();

        while (dr.Read())
        {
            textBox2.Text = dr.GetInt32(0).ToString();
            //textBox1.Text = dr.GetInt32(1).ToString();
            id = Convert.ToInt32(textBox2.Text);
        }
        dr.Close();
        comck2 = 1;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (comck == 0 & comck2 == 0 || textBox3.Text == "")
        {
            MessageBox.Show("you must select or inter data","info");
        }
        else
        {
            con.Close();
            cmd = new SqlCommand("Insert into IMAGERY_DIAGNOSTIC(People_ID,Mg_Nam,Result) values ('" + id + "','" + idx + "','" + textBox3.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Added new IMAGERY DIAGNOSTIC ", "ADD", MessageBoxButtons.OK, MessageBoxIcon.Information);

            dataGridView1.DataSource = null;

            con.Close();
            readdatagrid1();
        }
           // MessageBox.Show("info save"+comboBox1.SelectedIndex," info ");

    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (selectgrid == 0)
        { MessageBox.Show("you must select record to delet it","Info",MessageBoxButtons.OK,MessageBoxIcon.Information); }
        else
        {
            cmd = new SqlCommand("Delete From IMAGERY_DIAGNOSTIC Where Img_ID='" + bindex + "'", con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("You delete recored successfully", "delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dataGridView1.DataSource = null;
            con.Close();
            readdatagrid1();
        }
    }

    public int bindex;
    public int selectgrid = 0;
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
            textBox1.Text = row.Cells["People_ID"].Value.ToString();   //roll type to update
            comboBox2.Text = row.Cells["Name"].Value.ToString();
            textBox3.Text = row.Cells["Result"].Value.ToString();
            string dex = row.Cells["Img_ID"].Value.ToString();
            bindex = Convert.ToInt32(dex);  //Bld_ID in integer format                    | for Query

            selectgrid = 1;
        }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

替换以下代码dataGridView1.DataSource = new BindingSource { DataSource =dpep.Tables["IMAGERY_DIAGNOSTIC"] };

...oShortDateString() + "', '" + textBox1.Text.Trim() + "', " + label5.T...