为什么datagridview单元格单击事件显示过滤后第一行的图像?

时间:2016-03-13 21:03:46

标签: c# winforms datagridview

我正在使用datagridview_cellClick事件来显示单击时行的记录,这样可以正常工作。但问题是在搜索到特定行后,一旦点击查看该行的记录,图像列就会显示第一行的第一个图像。虽然除了图像之外显示该行的其余记录。换句话说,除了图像之外,还有正确的详细信息,如名称出生日期等。在搜索

后单击时,显示的图像是针对该特定行的第一行

以下是CellClick事件

的代码
   private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        OleDbCommand command = new OleDbCommand();
        command.Connection = conn;
        string query = "select spn_code,First_Name,Last_Name,Middle_Name,Date_Of_Birth,[Gender],[School],[Village],[Siblings],[Guardian],Contact_1,Contact_2,[Level],Life_Centre,Entry_Date,[Photo] from Child_Care_T1";
        command.CommandText = query;
        OleDbDataAdapter da = new OleDbDataAdapter(command);
        dt = new DataTable();
        da.Fill(dt);
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
            return;

        this.lbl_spn.Text = "SPN CODE: " + this.dataGridView1.Rows[e.RowIndex].Cells["spn_code"].Value.ToString();
       try
       {
           if (!Convert.IsDBNull(dt.Rows[e.RowIndex]["Photo"]))
            {
                // bytes = (byte[])r["Photo"];
                byte[] mydata = (byte[])dt.Rows[e.RowIndex]["Photo"];
                MemoryStream stream = new MemoryStream(mydata);
                this.pictureBox_AD.Image = Image.FromStream(stream);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Please click on Rows with data to view on side bar");
        }


        // MemoryStream stream = new MemoryStream(mydata);
        //this.pictureBox_AD.Image = Image.FromStream(stream);
        this.lbl_fname.Text ="First Name: " + this.dataGridView1.Rows[e.RowIndex].Cells["First_Name"].Value.ToString();
        this.lbl_lname.Text ="Last Name: "+ this.dataGridView1.Rows[e.RowIndex].Cells["Last_Name"].Value.ToString();
        this.lbl_mname.Text ="Middle Name: "+ this.dataGridView1.Rows[e.RowIndex].Cells["Middle_Name"].Value.ToString();
        this.lbl_DOB.Text ="Date Of Birth: " + this.dataGridView1.Rows[e.RowIndex].Cells["Date_Of_Birth"].Value.ToString();
        this.lbl_gender.Text ="Gender: " + this.dataGridView1.Rows[e.RowIndex].Cells["Gender"].Value.ToString();
        this.lbl_school.Text ="School: " + this.dataGridView1.Rows[e.RowIndex].Cells["School"].Value.ToString();
        this.lbl_villge.Text ="Village: " + this.dataGridView1.Rows[e.RowIndex].Cells["Village"].Value.ToString();
        this.lbl_siblings.Text ="No. of Siblings: " + this.dataGridView1.Rows[e.RowIndex].Cells["Siblings"].Value.ToString();
        this.lbl_guardian.Text ="Guardian: " + this.dataGridView1.Rows[e.RowIndex].Cells["Guardian"].Value.ToString();
        this.lbl_contact1.Text ="Guardian Contact 1: " + this.dataGridView1.Rows[e.RowIndex].Cells["Contact_1"].Value.ToString();
        this.lbl_contact2.Text = "Guardian Contact 2: " + this.dataGridView1.Rows[e.RowIndex].Cells["Contact_2"].Value.ToString();
        this.lbl_level.Text = "Level: " + this.dataGridView1.Rows[e.RowIndex].Cells["Level"].Value.ToString();
        this.lbl_life_centre.Text = "Life Centre: " + this.dataGridView1.Rows[e.RowIndex].Cells["Life_Centre"].Value.ToString();
        this.lbl_entryDate.Text ="Entry Date: " + this.dataGridView1.Rows[e.RowIndex].Cells["Entry_Date"].Value.ToString();
    }

}

这是搜索的代码

private void txt_search_TextChanged(object sender, EventArgs e)
    {
        DataView DatVlname = new DataView(dt);
        DatVlname.RowFilter = string.Format("[Last_Name] like '%{0}%' OR [First_Name] like '%{0}%' OR Life_Centre like '%{0}%' OR [Level] like '%{0}%'", txt_search.Text);
        dataGridView1.DataSource = DatVlname;
    }

这是表单加载的代码

 private void All_Details_Load(object sender, EventArgs e)
    {
        txt_search.Focus();

       // AddCheckBoxforDataGridView();
        try
        {
            conn.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = conn;
            command.CommandText = "select Details_Number,spn_code,First_Name,Last_Name,Middle_Name,Date_Of_Birth,[Gender],[School],[Village],[Siblings],[Guardian],Contact_1,Contact_2,[Level],Life_Centre,Entry_Date,[Photo] from Child_Care_T1";
            da = new OleDbDataAdapter(command);

            da.Fill(dt);

            dataGridView1.DataSource = dt;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Unable to Load Data" + ex);
        }
        conn.Close();
    }

`

0 个答案:

没有答案