-1

我的操作工作是当我单击单元格时gridview: 中的记录gridview将转到textbox和图片框。

当我单击 gridview 中的空单元格(datagridview 中没有记录)时,会发生此异常错误。

我分享我的操作图片

它来自错误是。这是我的代码:

  private void btn_picopen_Click(object sender, EventArgs e)
    {
        /*OpenFileDialog opf = new OpenFileDialog();
        opf.Filter = "Select Pic(*.JPG;*.png;.gif)|*.jpg;*.png;*.gif";
        if (opf.ShowDialog() == DialogResult.OK)
         {
            pic_staff.Image = Image.FromFile(opf.FileName);
        }*/
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.* ";
        if(dialog.ShowDialog() == DialogResult.OK)
        {
            imglocation = dialog.FileName.ToString();
            pic_staff.ImageLocation = imglocation;
        }
        


    }
private void btn_save_Click(object sender, EventArgs e)
    {

        if (Isvalid())
        {
            byte[] images = null;
            FileStream streem = new FileStream(imglocation, FileMode.Open, FileAccess.Read);
            BinaryReader brs = new BinaryReader(streem);
            images= brs.ReadBytes((int)streem.Length);

            SqlCommand cmd = new SqlCommand("INSERT INTO Add_New_Staff VALUES(@Staff_Name, @Father_Name, @City, @Address, @Mobile_No, @E_mail, @CNIC, @Education, @Subject, @Experience, @pic, @designation)", con);  //AddEsp ki jaga store procedure ka name
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@Staff_Name", txt_name.Text);
            cmd.Parameters.AddWithValue("@Father_Name", txt_fathername.Text);
            cmd.Parameters.AddWithValue("@City", txt_city.Text);
            cmd.Parameters.AddWithValue("@Address", txt_address.Text);
            cmd.Parameters.AddWithValue("@Mobile_No", txt_mobileno.Text);
            cmd.Parameters.AddWithValue("@E_mail", txt_email.Text);
            cmd.Parameters.AddWithValue("CNIC", txt_cnic.Text);
            cmd.Parameters.AddWithValue("@Education", txt_education.Text);
            cmd.Parameters.AddWithValue("@Subject", txt_subject.Text);
            cmd.Parameters.AddWithValue("@Experience", txt_experience.Text);
            cmd.Parameters.AddWithValue("@designation", txt_designation.Text);
            cmd.Parameters.AddWithValue("@pic", images);
            cmd.Parameters.AddWithValue("@Staff_Id", this.staff_Id);


            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("New Staff has been Added", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
            GetStudentsrecord();
            Clearformat();
        }
    }
 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {

        staff_Id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
        txt_name.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        txt_fathername.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
        txt_city.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
        txt_address.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
        txt_mobileno.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
        txt_email.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
        txt_cnic.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
        txt_education.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
        txt_experience.Text = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
        txt_subject.Text = dataGridView1.SelectedRows[0].Cells[10].Value.ToString();
        txt_designation.Text = dataGridView1.SelectedRows[0].Cells[11].Value.ToString();
        // pic_staff.Image = dataGridView1.SelectedRows[0].Cells[10].Value as Image;
        byte[] bytes = (byte[])dataGridView1.SelectedRows[0].Cells[12].Value;
        MemoryStream ms = new MemoryStream(bytes);
        pic_staff.Image = Image.FromStream(ms);


    }
4

2 回答 2

0

您可以在执行所有这些代码之前检查第一列

if (dataGridView1.SelectedRows[0].Cells[0].Value != null)
{
    staff_Id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
    ......
}

或者您可以取消选中网格的启用添加属性以消除新的空行。 在此处输入图像描述

于 2020-09-10T00:19:23.350 回答
0

如果只想检测一行的数据,可以使用datagirdview.CurrentRow属性。

您可以使用Convert.IsDBNull方法检查该值是否为DBnull.

这是代码示例可能会对您有所帮助。

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((!Convert.IsDBNull(dataGridView1.CurrentRow.Cells[1].Value)))
            {
                byte[] bytes = (byte[])dataGridView1.CurrentRow.Cells[1].Value;
                Image image = byteArrayToImage(bytes);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = image;
            }
            else
            {
                MessageBox.Show("there is no image in the row, please click aonther row");
            }
        }

       
        //convert bytearray to image
        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            using (MemoryStream mStream = new MemoryStream(byteArrayIn))
            {
                return Image.FromStream(mStream);
            }
        }

结果:

在此处输入图像描述

于 2020-09-10T02:23:04.863 回答