C# 从sql server在dataGridview中显示不同的映像

C# 从sql server在dataGridview中显示不同的映像,c#,.net,sql-server,vb.net,C#,.net,Sql Server,Vb.net,我在sql server中有一个表,其中有一列有图像,我通过在Gridview中添加图像列来检索图像。如果用户单击图像列,应用程序将转到数据库,并检查该行在图像列中是否有数据,将以新的形式显示图片,一切都很完美 这是代码: 这是我如何填写网格视图的id: Sub FillGridd() Dim cbb As New DataGridViewComboBoxColumn() With {.HeaderText = "currency"} cbb.Items.Add("shekel"

我在sql server中有一个表,其中有一列有图像,我通过在
Gridview
中添加图像列来检索图像。如果用户单击图像列,应用程序将转到数据库,并检查该行在图像列中是否有数据,将以新的形式显示图片,一切都很完美 这是代码:

这是我如何填写
网格视图的id

Sub FillGridd()
    Dim cbb As New DataGridViewComboBoxColumn() With {.HeaderText = "currency"}
    cbb.Items.Add("shekel")
    cbb.Items.Add("dinar")
    cbb.Items.Add("dollar")
    cbb.Items.Add("euro")
    cbb.Items.Add("yuan")
    cbb.FlatStyle = FlatStyle.Flat
    'Delete Button
    Dim btnDelete As New DataGridViewButtonColumn()
    btnDelete.FlatStyle = FlatStyle.Flat
    'Image Button
    Dim btnImage As New DataGridViewButtonColumn()
    btnDelete.FlatStyle = FlatStyle.Popup

    Dim column = New CalendarColumn() With {.HeaderText = "Date"}
    column.DefaultCellStyle.Format = "dd/MM/yyyy h:mm:ss tt"
    column.DataPropertyName = "Date"

    MetroGrid2.Columns.Insert(0, btnDelete)
    MetroGrid2.Columns.Add("", "المبلغ المرسل")
    MetroGrid2.Columns.Add(cbb)
    MetroGrid2.Columns.Add("", "Commission")
    MetroGrid2.Columns.Add("", "receiver name")
    MetroGrid2.Columns.Add("", "receiver phone")
    MetroGrid2.Columns.Add("", "Recipient country")
    MetroGrid2.Columns.Add("", "sender's name")
    MetroGrid2.Columns.Add("", "sender's phone")
    MetroGrid2.Columns.Add("", "the recipient company")
    'MetroGrid2.Columns.Add("", "Date")
    MetroGrid2.Columns.Add(column)
    MetroGrid2.Columns.Add(btnImage)
    MetroGrid2.Columns.Add("", "Notes")
    MetroGrid2.Columns.Add("", "ID")
    MetroGrid2.Columns(13).Visible = False


    Const sqlstring As String = "SELECT * FROM money_sent WHERE date >= DATEADD(DAY, -30, GETDATE()) ORDER BY Date DESC"

    If SQL.conn.State = ConnectionState.Open Then
        SQL.conn.Close()
    End If
    SQL.conn.Open()
    Dim adt As New SqlDataAdapter(sqlstring, SQL.conn)
    Dim dt As New DataTable
    adt.Fill(dt)
    Dim i As Integer
    For i = 0 To dt.Rows.Count - 1
        MetroGrid2.Rows.Add()
        MetroGrid2.Rows(i).Cells(1).Value = dt.Rows(i).ItemArray(1)
        Dim cell As DataGridViewComboBoxCell = DirectCast(MetroGrid2.Rows(i).Cells(2), DataGridViewComboBoxCell)
        cell.Value = dt.Rows(i).ItemArray(2)
        MetroGrid2.Rows(i).Cells(3).Value = dt.Rows(i).ItemArray(3)
        MetroGrid2.Rows(i).Cells(4).Value = dt.Rows(i).ItemArray(4)
        'DataGridView1.Rows(i).Cells(4).Value = "DELETE"
        MetroGrid2.Rows(i).Cells(5).Value = dt.Rows(i).ItemArray(5)
        MetroGrid2.Rows(i).Cells(6).Value = dt.Rows(i).ItemArray(6)
        MetroGrid2.Rows(i).Cells(7).Value = dt.Rows(i).ItemArray(7)
        MetroGrid2.Rows(i).Cells(8).Value = dt.Rows(i).ItemArray(8)
        MetroGrid2.Rows(i).Cells(9).Value = dt.Rows(i).ItemArray(9)
        MetroGrid2.Rows(i).Cells(10).Value = dt.Rows(i).ItemArray(10)
        MetroGrid2.Rows(i).Cells(12).Value = dt.Rows(i).ItemArray(12)
        MetroGrid2.Rows(i).Cells(13).Value = dt.Rows(i).ItemArray(0)
    Next
    SQL.conn.Close()

    'MetroGrid2.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
    MetroGrid2.CellBorderStyle = DataGridViewCellBorderStyle.Single
    Me.MetroGrid2.GridColor = Color.WhiteSmoke
End Sub
     Private Sub MetroGrid2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles MetroGrid2.CellContentClick
    id = MetroGrid2.SelectedRows(0).Cells(13).Value
    If e.ColumnIndex = 11 Then
            Dim dr As SqlDataReader
            Dim cmd As SqlCommand
            Dim stream As New MemoryStream()
            Dim query As String
            query = "select * from money_sent where id='" & id & "'"

            If SQL.conn.State = ConnectionState.Open Then
                SQL.conn.Close()
            End If
            SQL.conn.Open()
            cmd = New SqlCommand(query, SQL.conn)
            'dr = Cmd.ExecuteReader
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While dr.Read
                If dr("idpicture").ToString() <> "" Then
                    Dim image As [Byte]() = DirectCast(dr("idpicture"), [Byte]())
                    stream.Write(image, 0, image.Length)
                    Dim bitmap As New Bitmap(stream)
                    'Form3.PictureBox1.Image = bitmap
                    Dim frm As New Form3
                    frm.PictureBox1.Image = bitmap
                    frm.id = id
                    frm.sname = MetroGrid2.SelectedRows(0).Cells(7).Value
                    frm.ShowDialog()
                Else
                    noimage = "no"
                End If
            End While
            SQL.conn.Close()

            If noimage = "no" Then
                If MessageBox.Show("there is no image for this record do you want add image ?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                    UpdateImage()
                End If
            End If
        End If
End Sub
这就是我在
Gridview
中添加可点击图像的方式:

Sub FillGridd()
    Dim cbb As New DataGridViewComboBoxColumn() With {.HeaderText = "currency"}
    cbb.Items.Add("shekel")
    cbb.Items.Add("dinar")
    cbb.Items.Add("dollar")
    cbb.Items.Add("euro")
    cbb.Items.Add("yuan")
    cbb.FlatStyle = FlatStyle.Flat
    'Delete Button
    Dim btnDelete As New DataGridViewButtonColumn()
    btnDelete.FlatStyle = FlatStyle.Flat
    'Image Button
    Dim btnImage As New DataGridViewButtonColumn()
    btnDelete.FlatStyle = FlatStyle.Popup

    Dim column = New CalendarColumn() With {.HeaderText = "Date"}
    column.DefaultCellStyle.Format = "dd/MM/yyyy h:mm:ss tt"
    column.DataPropertyName = "Date"

    MetroGrid2.Columns.Insert(0, btnDelete)
    MetroGrid2.Columns.Add("", "المبلغ المرسل")
    MetroGrid2.Columns.Add(cbb)
    MetroGrid2.Columns.Add("", "Commission")
    MetroGrid2.Columns.Add("", "receiver name")
    MetroGrid2.Columns.Add("", "receiver phone")
    MetroGrid2.Columns.Add("", "Recipient country")
    MetroGrid2.Columns.Add("", "sender's name")
    MetroGrid2.Columns.Add("", "sender's phone")
    MetroGrid2.Columns.Add("", "the recipient company")
    'MetroGrid2.Columns.Add("", "Date")
    MetroGrid2.Columns.Add(column)
    MetroGrid2.Columns.Add(btnImage)
    MetroGrid2.Columns.Add("", "Notes")
    MetroGrid2.Columns.Add("", "ID")
    MetroGrid2.Columns(13).Visible = False


    Const sqlstring As String = "SELECT * FROM money_sent WHERE date >= DATEADD(DAY, -30, GETDATE()) ORDER BY Date DESC"

    If SQL.conn.State = ConnectionState.Open Then
        SQL.conn.Close()
    End If
    SQL.conn.Open()
    Dim adt As New SqlDataAdapter(sqlstring, SQL.conn)
    Dim dt As New DataTable
    adt.Fill(dt)
    Dim i As Integer
    For i = 0 To dt.Rows.Count - 1
        MetroGrid2.Rows.Add()
        MetroGrid2.Rows(i).Cells(1).Value = dt.Rows(i).ItemArray(1)
        Dim cell As DataGridViewComboBoxCell = DirectCast(MetroGrid2.Rows(i).Cells(2), DataGridViewComboBoxCell)
        cell.Value = dt.Rows(i).ItemArray(2)
        MetroGrid2.Rows(i).Cells(3).Value = dt.Rows(i).ItemArray(3)
        MetroGrid2.Rows(i).Cells(4).Value = dt.Rows(i).ItemArray(4)
        'DataGridView1.Rows(i).Cells(4).Value = "DELETE"
        MetroGrid2.Rows(i).Cells(5).Value = dt.Rows(i).ItemArray(5)
        MetroGrid2.Rows(i).Cells(6).Value = dt.Rows(i).ItemArray(6)
        MetroGrid2.Rows(i).Cells(7).Value = dt.Rows(i).ItemArray(7)
        MetroGrid2.Rows(i).Cells(8).Value = dt.Rows(i).ItemArray(8)
        MetroGrid2.Rows(i).Cells(9).Value = dt.Rows(i).ItemArray(9)
        MetroGrid2.Rows(i).Cells(10).Value = dt.Rows(i).ItemArray(10)
        MetroGrid2.Rows(i).Cells(12).Value = dt.Rows(i).ItemArray(12)
        MetroGrid2.Rows(i).Cells(13).Value = dt.Rows(i).ItemArray(0)
    Next
    SQL.conn.Close()

    'MetroGrid2.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
    MetroGrid2.CellBorderStyle = DataGridViewCellBorderStyle.Single
    Me.MetroGrid2.GridColor = Color.WhiteSmoke
End Sub
     Private Sub MetroGrid2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles MetroGrid2.CellContentClick
    id = MetroGrid2.SelectedRows(0).Cells(13).Value
    If e.ColumnIndex = 11 Then
            Dim dr As SqlDataReader
            Dim cmd As SqlCommand
            Dim stream As New MemoryStream()
            Dim query As String
            query = "select * from money_sent where id='" & id & "'"

            If SQL.conn.State = ConnectionState.Open Then
                SQL.conn.Close()
            End If
            SQL.conn.Open()
            cmd = New SqlCommand(query, SQL.conn)
            'dr = Cmd.ExecuteReader
            dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While dr.Read
                If dr("idpicture").ToString() <> "" Then
                    Dim image As [Byte]() = DirectCast(dr("idpicture"), [Byte]())
                    stream.Write(image, 0, image.Length)
                    Dim bitmap As New Bitmap(stream)
                    'Form3.PictureBox1.Image = bitmap
                    Dim frm As New Form3
                    frm.PictureBox1.Image = bitmap
                    frm.id = id
                    frm.sname = MetroGrid2.SelectedRows(0).Cells(7).Value
                    frm.ShowDialog()
                Else
                    noimage = "no"
                End If
            End While
            SQL.conn.Close()

            If noimage = "no" Then
                If MessageBox.Show("there is no image for this record do you want add image ?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                    UpdateImage()
                End If
            End If
        End If
End Sub
Private Sub MetroGrid2\u CellContentClick(发送者作为对象,e作为DataGridViewCellEventArgs)处理MetroGrid2.CellContentClick
id=MetroGrid2.SelectedRows(0).单元格(13).值
如果e.ColumnIndex=11,则
将dr设置为SqlDataReader
Dim cmd作为SqlCommand
将流变暗为新内存流()
将查询设置为字符串
query=“select*from MONY\发送到id=”&id&“
如果SQL.conn.State=ConnectionState.Open,则
SQL.conn.Close()
如果结束
SQL.conn.Open()
cmd=newsqlcommand(查询,SQL.conn)
'dr=Cmd.ExecuteReader
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection)
当雷德博士
如果dr(“idpicture”).ToString()“”那么
Dim图像为[Byte]()=DirectCast(dr(“idpicture”),[Byte]())
stream.Write(image,0,image.Length)
将位图变暗为新位图(流)
'Form3.PictureBox1.Image=位图
将frm变暗为新表格3
frm.PictureBox1.Image=位图
frm.id=id
frm.sname=MetroGrid2.SelectedRows(0).单元格(7).值
frm.ShowDialog()
其他的
noimage=“否”
如果结束
结束时
SQL.conn.Close()
如果noimage=“否”,则
如果MessageBox.Show(“此记录没有图像,是否要添加图像?”,“信息”,MessageBoxButtons.YesNo,MessageBoxIcon.Question)=DialogResult.Yes,则
UpdateImage()
如果结束
如果结束
如果结束
端接头
这就是所有的代码,现在我想要的是如何在
Gridview
image列中为数据库中有图片的行显示不同的图像,以便用户能够区分有图片的行和没有图片的行

这是
网格视图中图像列的显示方式:

您是否在询问,当没有与记录关联的图像时,如何显示一些标准图像?你不需要两张这样的图片吗,一张是“点击查看”,一张是“点击查看无图片”?是的,我已经做了“是”点击查看,一张是“无图片点击查看”,当记录中没有图片时,我想显示默认图片,当记录中有图片时,显示另一张图片,事实上,是的,这就是我需要的,就像你说的那样,为DefaultCellStyle设置nonone为null值属性,或者使用Image列的Image属性。注意:有更简单的方法来填充DGV,即使其中一列是自定义列show me please,因为这种方法需要时间来显示所有数据,并且程序会变成黑屏显示数据,并将其作为投票给您的答案谢谢。