Gridview,更改可见值

时间:2013-06-12 14:07:43

标签: c# datagridview visual-studio-2005

我有一个gridview,它绑定了数据库中的值。每行中有一个单元存储的值如下:

1

2

3

4

最好的方法是更改​​那些不在数据库中但仅限于查看

的值的方法

Q1

年中

Q3

结束年度

3 个答案:

答案 0 :(得分:0)

dataGridView.Rows[0].Cells[0].Value = desiredValue
dataGridView.Rows[1].Cells[0].Value = desiredValue
dataGridView.Rows[2].Cells[0].Value = desiredValue
dataGridView.Rows[3].Cells[0].Value = desiredValue

或者你可以循环浏览它们。您可以访问gridView的各个数据和平,如nameOfGridView.Rows[desiredRow].Cells[desiredColumn].Value

答案 1 :(得分:0)

您可以在CellFormatting事件中尝试...

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    Dim sHeader As String = DataGridView1.Columns(e.ColumnIndex).Name

    If ucase(sHeader) = "QUARTER" Then '-------> change this with yours
        If e IsNot Nothing Then
            If e.Value IsNot Nothing Then
                Try
                    Select case e.Value
                        Case 1 : e.Value = "Q1"
                        Case 2 : e.Value = "Mid Year"
                        Case 3 : e.Value = "Q2"
                        Case 4 : e.Value = "End Year"
                    End Select
                    e.FormattingApplied = True
                Catch ex As FormatException
                    e.Value = ""
                End Try
            End If
        End If
    End If

End Sub

答案 2 :(得分:0)

谢谢你们,但我能够用它来获得它:

    protected string QuarterConvert(object strValue)
    {
        string retString = "";

        switch (Convert.ToString(strValue))
        {
            case "1":
                retString = "Q1";
                break;
            case "2":
                retString = "Mid-Year";
                break;
            case "3":
                retString = "Q3";
                break;
            case "4":
                retString = "Year-End";
                break;
            default:
                break;

        }
        return retString;
    }