C# Datagridview Intersection Cell Formatting Problem

35 views Asked by At

I've a datagridview that name as you can see in the below. I'd like to set readonly and set background color only intersection cells.

But my formatting method is not working correctly as I want. I want to format only intersection cells not all column cells. Actually it's working but not only intersection cell. How can I fix my method to format only row and column intersection cells. Thanks a lot.

How to fix my method to format only intersection cells?

private void roomSizesInformationsDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

    int[] firstTtargetColumns = { 7, 8, 9, 10 }; /*1, 2, 9, 10, 11, 12, 13, 20, 21, 22, 25, 26, 27 Nolu Hesap Türleri İçin*/
    int[] secondTargetColumns = { }; /*3 ve 6 Nolu Hesap Türleri İçin Herhangi Bir Kolon Sınırlaması Bulunmamaktadır.*/
    int[] thirdTtargetColumns = { 8, 9 }; /*4 ve 7 Nolu Hesap Türleri İçin*/
    int[] fourthTtargetColumns = { 6, 7, 9 }; /*4 ve 7 Nolu Hesap Türleri İçin*/
    int[] fifthTtargetColumns = { 6, 7, 8, 9, 10 }; /*14, 15, 16, 17, 23, 24, 34, 35 Nolu Hesap Türleri İçin*/

    Dictionary<string, int[]> firstTargetColumnsByCalculationType = new Dictionary<string, int[]>
        {
            {"Mahal Döşeme Alanı", firstTtargetColumns},
            {"Mahal Çevre Uzunluğu", firstTtargetColumns},
            {"Serbest Dairesel Kolon", firstTtargetColumns},
            {"Serbest Kare Kolon", firstTtargetColumns},
            {"Serbest Dikdörtgen Kolon", firstTtargetColumns},
            {"Çokgen Kolon Alanı", firstTtargetColumns},
            {"Çokgen Kolon Çevresi", firstTtargetColumns},
            {"Minha Duvar Kaplaması", firstTtargetColumns},
            {"Serbest Duvar", firstTtargetColumns},
            {"İlave İskele", firstTtargetColumns},
            {"Minha İskele", firstTtargetColumns},
            {"Düşük Kotlu Duvar", firstTtargetColumns}
        };
    Dictionary<string, int[]> thirdTargetColumnsByCalculationType = new Dictionary<string, int[]>
        {
            {"Minha Edilecek Kapı", thirdTtargetColumns},
            {"Minha Edilecek Pencere", thirdTtargetColumns},
        };
    Dictionary<string, int[]> fourthTargetColumnsByCalculationType = new Dictionary<string, int[]>
        {
            {"Dahil Edilecek Kapı", fourthTtargetColumns},
            {"Dahil Edilecek Pencere", fourthTtargetColumns},
        };
    Dictionary<string, int[]> fifthTargetColumnsByCalculationType = new Dictionary<string, int[]>
        {
            {"İlave Döşeme", fifthTtargetColumns},
            {"Minha Döşeme", fifthTtargetColumns},
            {"Minha Merdiven Boşluğu", fifthTtargetColumns},
            {"Minha Merdiven Basamağı", fifthTtargetColumns},
            {"İlave Tavan Kaplaması", fifthTtargetColumns},
            {"Minha Tavan Kaplaması", fifthTtargetColumns},
            {"Minha Merdiven Boşluğu Döşeme", fifthTtargetColumns},
            {"Minha Merdiven Boşluğu Tavan", fifthTtargetColumns},
        };

    if (firstTtargetColumns.Contains(e.ColumnIndex) && e.RowIndex >= 0)
    {
        DataGridViewComboBoxColumn comboColumn = roomSizesInformationsDataGridView.Columns["calculationType"] as DataGridViewComboBoxColumn;

        if (comboColumn != null)
        {
            object calculationTypeDisplayValueObj = roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells["calculationType"].FormattedValue;

            if (calculationTypeDisplayValueObj != null)
            {
                string calculationTypeDisplayValue = calculationTypeDisplayValueObj.ToString();

                if (firstTargetColumnsByCalculationType.ContainsKey(calculationTypeDisplayValue))
                {
                    int[] targetColumns = firstTargetColumnsByCalculationType[calculationTypeDisplayValue];

                    foreach (int columnIndex in targetColumns)
                    {
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].ReadOnly = true;
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].Style.BackColor = Color.Black;
                    }
                }

                else if (thirdTargetColumnsByCalculationType.ContainsKey(calculationTypeDisplayValue))
                {
                    int[] targetColumns = thirdTargetColumnsByCalculationType[calculationTypeDisplayValue];

                    foreach (int columnIndex in targetColumns)
                    {
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].ReadOnly = true;
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].Style.BackColor = Color.Black;
                    }
                }
                else if (fourthTargetColumnsByCalculationType.ContainsKey(calculationTypeDisplayValue))
                {
                    int[] targetColumns = fourthTargetColumnsByCalculationType[calculationTypeDisplayValue];

                    foreach (int columnIndex in targetColumns)
                    {
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].ReadOnly = true;
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].Style.BackColor = Color.Black;
                    }
                }
                else if (fifthTargetColumnsByCalculationType.ContainsKey(calculationTypeDisplayValue))
                {
                    int[] targetColumns = fifthTargetColumnsByCalculationType[calculationTypeDisplayValue];

                    foreach (int columnIndex in targetColumns)
                    {
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].ReadOnly = true;
                        roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells[columnIndex].Style.BackColor = Color.Black;
                    }
                }
                else
                {
                    // Siyah arka planı ayarlanmayan hücrelerin arka plan rengini temizleme
                    foreach (DataGridViewCell cell in roomSizesInformationsDataGridView.Rows[e.RowIndex].Cells)
                    {
                        if (!firstTtargetColumns.Contains(cell.ColumnIndex))
                        {
                            cell.ReadOnly = false;
                            cell.Style.BackColor = roomSizesInformationsDataGridView.DefaultCellStyle.BackColor = Color.White; // Varsayılan arka plan rengi
                        }
                    }
                }

            }
        }
    }
}
0

There are 0 answers