在数据网格视图中将图像分配给行

时间:2014-05-12 19:33:14

标签: c# winforms visual-studio-2010 datagridview

我有一个使用VS 2010用C#编写的winform程序。

我通过单击一个来编辑数据网格视图中的行,从行中获取该数据并使用它填充文本框,根据需要编辑发送回我的数据网格。

我想要的是有一个与我的数据网格视图中的每一行相关的图像(例如,员工的图片),当选择该特定行时,该图像将显示在图片框中。

简而言之,如果我点击员工Andrew,安德鲁的图片会出现,如果我点击Rasheed,Rasheeds图片就会出现等等。

图片框位于数据网格视图之外,因此不在另一列中。它只是孤立无援。

有人可以给我一些帮助吗?

选择行和发送到文本框的代码:

// Displays Customer infomation in Update Area
        if (e.RowIndex < 0 || e.ColumnIndex < 0)
        {

        }
        else
        {
            if (customersDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
            {
                textBox_customerID.Text = "" + customersDataGridView.Rows[e.RowIndex].Cells["Customer_ID"].Value.ToString();
                textBox_forename.Text = customersDataGridView.Rows[e.RowIndex].Cells["Forename"].Value.ToString();
                textBox_surname.Text = customersDataGridView.Rows[e.RowIndex].Cells["Surname"].Value.ToString();
                textBox_address.Text = customersDataGridView.Rows[e.RowIndex].Cells["Address"].Value.ToString();
                textBox_town.Text = customersDataGridView.Rows[e.RowIndex].Cells["Town"].Value.ToString();
                textBox_postcode.Text = customersDataGridView.Rows[e.RowIndex].Cells["Postcode"].Value.ToString();
                textBox_dateofbirth.Text = customersDataGridView.Rows[e.RowIndex].Cells["Date_Of_Birth"].Value.ToString();
                textBox_phonenum.Text = customersDataGridView.Rows[e.RowIndex].Cells["Phone_Number"].Value.ToString();
                textBox_email.Text = customersDataGridView.Rows[e.RowIndex].Cells["Email"].Value.ToString();
                textBox_current_rental.Text = customersDataGridView.Rows[e.RowIndex].Cells["Current_Rental"].Value.ToString();
            }   

更新行和发送回数据网格的代码:

DialogResult dialogResult = MessageBox.Show("Are you sure you want to update this customer? ", "Update - " + textBox_forename.Text + textBox_surname.Text, MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {// Updates Product Info
            if (textBox_customerID.Text == "" || textBox_forename.Text == "" || textBox_surname.Text == "" || textBox_address.Text == "" || textBox_town.Text == "" || textBox_postcode.Text == "" || textBox_dateofbirth.Text == "" || textBox_phonenum.Text == "" || textBox_email.Text == "" || textBox_current_rental.Text == "")
            {
                MessageBox.Show("Please leave no boxes blank");
            }
            else
            {
                string custData = textBox_customerID.Text;
                command.CommandText = "UPDATE customers SET Customer_ID = '" + textBox_customerID.Text + "',  Forename = '" + textBox_forename.Text + "', Surname = '" + textBox_surname.Text + "', Address = '"
                    + textBox_address.Text + "', Town = '" + textBox_town.Text + "', Postcode = '" + textBox_postcode.Text + "', Date_Of_Birth = '" + textBox_dateofbirth.Text + "', Phone_Number = '" + textBox_phonenum.Text + "', Email = '" + textBox_email.Text + "', Current_Rental = '" + textBox_current_rental.Text + "' WHERE Customer_ID = '" + custData + "'";
                connect.Open();
                command.Connection = connect;
                command.ExecuteNonQuery();
                connect.Close();
                customersDataGridView.Rows.Clear();
                refreshCustomers();
                MessageBox.Show("Customer Updated");

1 个答案:

答案 0 :(得分:1)

选项1

您可以在DataGridView中添加DataGridViewImageColumn以显示该行中的员工图片。 MSDN示例在上面指定的链接

中非常有用

选项2

  1. 跟踪CellClick活动
  2. 获取行索引int rowIndex = e.RowIndex
  3. 获取DataGridViewRow DataGridViewRow row = DataGridView1.Rows [rowIndex]
  4. 根据行值从数据源检索数据
  5. 从检索到的数据中将图片加载到图片框中。
  6. Byte[]中检索到的图像数据可以加载到图片框中。这样的事情:

    var data = (Byte[])(datatable.Rows[rowIndex]["EmployeeImage"]);
    var stream = new MemoryStream(data);
    pictureBox1.Image= Image.FromStream(sream);