[WinForm]DataGridView之CellFormatting

       private void gcConfigShow_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

        {

            if (e.RowIndex < 0) return;

            DataGridView _dgv = (DataGridView)sender;

            DataGridViewCell _cell = _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];

            string _cellName = _dgv.Columns[e.ColumnIndex].Name;

            try

            {

                switch (_cellName)

                {

                    case "OpWay":

                        string _cellOpWay = e.Value.ToString().Trim();

                        e.Value = _cellOpWay == "00" ? "关" : _cellOpWay == "01" ? "开" : "节一模式";

                        e.FormattingApplied = true;

                        break;

                    case "OpCtuCh":

                        string _cellCtuch = e.Value.ToString().Trim();

                        StringBuilder _sbCtuch = new StringBuilder();

                        for (int i = 0; i < _cellCtuch.Length; i++)

                        {

                            if (_cellCtuch[i].Equals('1'))

                                _sbCtuch.AppendFormat("回路{0} ", i + 1);

                        }

                        e.Value = _sbCtuch.ToString();

                        e.FormattingApplied = true;

                        break;



                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("格式化表信息异常,原因:{0}", ex.Message.Trim());

            }

        }

你可能感兴趣的:(datagridview)