コード例 #1
0
        public void OnChange()
        {
            if (rows.Count > 0)
            {
                if (rows?[0].cells.Count == columns?.Count)
                {
                    if (uiRows.Count > 0)
                    {
                        ClearRows();
                    }

                    for (int i = 0; i < rows.Count; i++)
                    {
                        DataGridViewRow   row       = rows[i];
                        GameObject        rowObject = DataGridViewRowUI.CreateRow();
                        DataGridViewRowUI rowUI     = rowObject.GetComponent <DataGridViewRowUI>();

                        for (int j = 0; j < row.cells.Count; j++)
                        {
                            GameObject cell = DataGridViewCellUI.CreateCell();

                            Image cellImage = cell.GetComponent <Image>();
                            if (colorRows)
                            {
                                cellImage       = cell.GetComponent <Image>();
                                cellImage.color = i == 0 || i % 2 == 0 ? even : odd;
                            }
                            else
                            {
                                cellImage.color = even;
                            }

                            RectTransform cellRT   = cell.GetComponent <RectTransform>();
                            RectTransform headerRT = columns[j].GetComponent <RectTransform>();
                            cellRT.sizeDelta = new Vector2(headerRT.sizeDelta.x, cellRT.sizeDelta.y);

                            cell.transform.SetParent(rowUI.transform);
                            DataGridViewCellUI cellUI = cell.GetComponent <DataGridViewCellUI>();

                            if (tableFont)
                            {
                                cellUI.textComponent.font = tableFont;
                            }

                            cellUI.textComponent.color = cellTextColor;
                            cellUI.textComponent.text  = row.cells[j].value;

                            Button cellButton          = cell.GetComponent <Button>();
                            DataGridViewEventArgs args = new DataGridViewEventArgs(i, j);
                            cellButton.onClick.AddListener(OnClick);

                            rowUI.cells.Add(cellUI);

                            void OnClick()
                            {
                                cellClicked.Invoke(args);
                                cellDoubleClicked.Invoke(args);
                            }
                        }


                        uiRows.Add(rowUI);
                    }

                    uiRows.ForEach(r =>
                    {
                        r.transform.SetParent(rowsComponent.transform);
                        RectTransform rowRT      = r.GetComponent <RectTransform>();
                        rowRT.anchoredPosition3D = Vector3.zero;
                        rowRT.localScale         = Vector3.one;
                        rowRT.localRotation      = Quaternion.Euler(Vector3.zero);
                    });
                }
                else
                {
                    throw new System.Exception("Cells count don't match with header!");
                }
            }
        }
コード例 #2
0
 void OnCellClicked(DataGridViewEventArgs args)
 {
     UnPaintRow(selectedRow);
     selectedRow = dataGrid.uiRows[args.row];
     PaintRow(selectedRow);
 }