2020年4月24日 星期五

如何讓DataGridView每筆Row呈現不同的Cell Type

參考來源: [C#]如何讓DataGridView每筆Row呈現不同的Cell Type
--
C#
{
    //如果是點在Value欄位的話才出現
    if (e.ColumnIndex == 1)
    {
        DataGridViewRow tNowRow = dataGridView1.Rows[dataGridView1.CurrentRow.Index];

        if (tNowRow.Cells["Type"].Value != null)
        {
            if (tNowRow.Cells["Type"].Value != null)
            {
                //如果是bool的話就變成CheckBox
                if (tNowRow.Cells["Type"].Value.ToString() == "bool")
                {
                    dataGridView1["Value", dataGridView1.CurrentRow.Index] = new DataGridViewCheckBoxCell();
                }
                //如果是combo的話就變成下拉選單
                else if (tNowRow.Cells["Type"].Value.ToString() == "combo")
                {
                    DataGridViewComboBoxCell tCell = new DataGridViewComboBoxCell();

                    for (int i = 0; i < tNowRow.Cells["AllowedValues"].Value.ToString().Split(';').Length; i++)
                    {
                        tCell.Items.Add(tNowRow.Cells["AllowedValues"].Value.ToString().Split(';')[i]);
                    }

                    dataGridView1["Value", dataGridView1.CurrentRow.Index] = tCell;
                }
            }
        }
    }
}

--
vbnet


If True Then
'如果是點在Value欄位的話才出現
If e.ColumnIndex = 1 Then
Dim tNowRow As DataGridViewRow = dataGridView1.Rows(dataGridView1.CurrentRow.Index)

If tNowRow.Cells("Type").Value IsNot Nothing Then
If tNowRow.Cells("Type").Value IsNot Nothing Then
'如果是bool的話就變成CheckBox
If tNowRow.Cells("Type").Value.ToString() = "bool" Then
dataGridView1("Value", dataGridView1.CurrentRow.Index) = New DataGridViewCheckBoxCell()
'如果是combo的話就變成下拉選單
ElseIf tNowRow.Cells("Type").Value.ToString() = "combo" Then
Dim tCell As New DataGridViewComboBoxCell()

Dim i As Integer = 0
While i < tNowRow.Cells("AllowedValues").Value.ToString().Split(";"C).Length
tCell.Items.Add(tNowRow.Cells("AllowedValues").Value.ToString().Split(";"C)(i))
i += 1
End While

dataGridView1("Value", dataGridView1.CurrentRow.Index) = tCell
End If
End If
End If
End If
End If

沒有留言:

張貼留言