6/18/2007

События DataGridView

Решил посмотреть на последовательность событий, которые создает DataGridView. В гриде всего один столбец и две строки с текстом.

 

DataTable table = new DataTable();

table.Columns.Add("key", typeof(string));

for (int i=0; i < 2; i++)

    table.Rows.Add("key" + i);

 

DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;

grid.Parent = this;

grid.DataSource = table;

grid.AllowUserToAddRows = false;

grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

 

С помощью UniversalEventHandler сгенерировал код, который ко всем найденным 187-и событиям подключил общий для всех обработчик. Запустил программу и получил от DataGridView следующие события:

 

0001: DockChanged

0002: ParentChanged

0003: VisibleChanged

0004: Layout

0005: Resize

0006: SizeChanged

0007: ClientSizeChanged

0008: ColumnAdded

0009: RowHeightInfoNeeded

0010: RowHeightInfoNeeded

0011: RowsAdded

0012: RowsRemoved

0013: RowHeightInfoNeeded

0014: RowHeightInfoNeeded

0015: RowsAdded

0016: RowHeightInfoNeeded

0017: RowsAdded

0018: RowHeightInfoNeeded

0019: RowsAdded

0020: BindingContextChanged

0021: DataBindingComplete

0022: RowsRemoved

0023: ColumnRemoved

0024: ColumnAdded

0025: RowHeightInfoNeeded

0026: RowHeightInfoNeeded

0027: RowsAdded

0028: RowsRemoved

0029: RowHeightInfoNeeded

0030: RowHeightInfoNeeded

0031: RowsAdded

0032: RowHeightInfoNeeded

0033: RowsAdded

0034: RowHeightInfoNeeded

0035: RowsAdded

0036: DataSourceChanged

0037: DataBindingComplete

0038: RowsRemoved

0039: AllowUserToAddRowsChanged

0040: AutoSizeColumnsModeChanged

0041: HandleCreated

0042: RowHeightInfoNeeded

0043: RowHeightInfoNeeded

0044: RowHeightInfoNeeded

0045: RowHeightInfoNeeded

0046: RowHeightInfoNeeded

0047: RowHeightInfoNeeded

0048: RowUnshared

0049: RowStateChanged

0050: RowUnshared

0051: RowStateChanged

0052: ColumnStateChanged

0053: Invalidated

0054: ColumnWidthChanged

0055: RowHeightInfoNeeded

0056: RowHeightInfoNeeded

0057: RowEnter

0058: CurrentCellChanged

0059: CellEnter

0060: RowHeightInfoNeeded

0061: RowHeightInfoNeeded

0062: Invalidated

0063: RowHeightInfoNeeded

0064: RowHeightInfoNeeded

0065: Invalidated

0066: RowHeightInfoNeeded

0067: RowHeightInfoNeeded

0068: Invalidated

0069: RowStateChanged

0070: SelectionChanged

0071: RowHeightInfoNeeded

0072: RowHeightInfoNeeded

0073: Invalidated

0074: RowStateChanged

0075: SelectionChanged

0076: CellLeave

0077: RowLeave

0078: CellFormatting

0079: CellValidating

0080: CellValidated

0081: RowValidating

0082: RowValidated

0083: ImeModeChanged

0084: CurrentCellChanged

0085: RowHeightInfoNeeded

0086: RowHeightInfoNeeded

0087: Invalidated

0088: RowHeightInfoNeeded

0089: RowHeightInfoNeeded

0090: Invalidated

0091: Invalidated

0092: RowsRemoved

0093: RowStateChanged

0094: RowStateChanged

0095: ColumnRemoved

0096: Invalidated

0097: ColumnStateChanged

0098: ColumnAdded

0099: ColumnStateChanged

0100: Invalidated

0101: Invalidated

0102: Invalidated

0103: RowHeightInfoNeeded

0104: RowHeightInfoNeeded

0105: RowHeightInfoNeeded

0106: RowHeightInfoNeeded

0107: RowHeightInfoNeeded

0108: RowUnshared

0109: RowStateChanged

0110: Invalidated

0111: RowsAdded

0112: RowEnter

0113: CurrentCellChanged

0114: CellEnter

0115: RowHeightInfoNeeded

0116: RowHeightInfoNeeded

0117: Invalidated

0118: RowHeightInfoNeeded

0119: RowHeightInfoNeeded

0120: Invalidated

0121: RowHeightInfoNeeded

0122: RowHeightInfoNeeded

0123: Invalidated

0124: RowStateChanged

0125: SelectionChanged

0126: RowHeightInfoNeeded

0127: RowHeightInfoNeeded

0128: RowHeightInfoNeeded

0129: RowHeightInfoNeeded

0130: RowHeightInfoNeeded

0131: RowHeightInfoNeeded

0132: RowHeightInfoNeeded

0133: RowHeightInfoNeeded

0134: RowHeightInfoNeeded

0135: RowUnshared

0136: RowStateChanged

0137: Invalidated

0138: RowsAdded

0139: Invalidated

0140: BindingContextChanged

0141: DataBindingComplete

0142: VisibleChanged

0143: Enter

0144: RowEnter

0145: CellEnter

0146: RowHeightInfoNeeded

0147: RowHeightInfoNeeded

0148: Invalidated

0149: GotFocus

0150: RowHeightInfoNeeded

0151: RowHeightInfoNeeded

0152: Invalidated

0153: Invalidated

0154: CellPainting

0155: CellPainting

0156: RowHeightInfoNeeded

0157: RowErrorTextNeeded

0158: RowPrePaint

0159: RowErrorTextNeeded

0160: CellPainting

0161: CellErrorTextNeeded

0162: CellFormatting

0163: CellPainting

0164: ChangeUICues

0165: Invalidated

0166: RowErrorTextNeeded

0167: RowPostPaint

0168: RowHeightInfoNeeded

0169: RowErrorTextNeeded

0170: RowPrePaint

0171: RowErrorTextNeeded

0172: CellPainting

0173: CellErrorTextNeeded

0174: CellFormatting

0175: CellPainting

0176: RowErrorTextNeeded

0177: RowPostPaint

0178: Paint

0179: CellPainting

0180: CellPainting

0181: RowHeightInfoNeeded

0182: RowErrorTextNeeded

0183: RowPrePaint

0184: RowErrorTextNeeded

0185: CellPainting

0186: CellErrorTextNeeded

0187: CellFormatting

0188: CellPainting

0189: RowErrorTextNeeded

0190: RowPostPaint

0191: RowHeightInfoNeeded

0192: RowErrorTextNeeded

0193: RowPrePaint

0194: RowErrorTextNeeded

0195: CellPainting

0196: CellErrorTextNeeded

0197: CellFormatting

0198: CellPainting

0199: RowErrorTextNeeded

0200: RowPostPaint

0201: Paint

 

На этом события прекратились. Строка «Key0» выделена. Жму на клавиатуре, например, на «d». В результате DataGridView посылает следующие события:

 

0202: PreviewKeyDown

0203: CellBeginEdit

0204: CellFormatting

0205: ControlAdded

0206: EditingControlShowing

0207: RowHeightInfoNeeded

0208: LostFocus

0209: RowHeightInfoNeeded

0210: RowHeightInfoNeeded

0211: Invalidated

0212: RowHeightInfoNeeded

0213: RowHeightInfoNeeded

0214: Invalidated

0215: RowHeightInfoNeeded

0216: RowHeightInfoNeeded

0217: Invalidated

0218: CurrentCellDirtyStateChanged

0219: RowHeightInfoNeeded

0220: RowErrorTextNeeded

0221: RowPrePaint

0222: RowErrorTextNeeded

0223: CellPainting

0224: CellErrorTextNeeded

0225: CellPainting

0226: RowErrorTextNeeded

0227: RowPostPaint

0228: RowHeightInfoNeeded

0229: Paint

 

Вместо «Key0» в текстовом редакторе отобразилась буква «d». Нажимаю «Enter». DataGridView посылает следующие события:

 

0230: CellLeave

0231: RowLeave

0232: CellValidating

0233: CellParsing

0234: RowHeightInfoNeeded

0235: RowHeightInfoNeeded

0236: Invalidated

0237: RowHeightInfoNeeded

0238: RowHeightInfoNeeded

0239: Invalidated

0240: CellValueChanged

0241: RowHeightInfoNeeded

0242: RowHeightInfoNeeded

0243: Invalidated

0244: CurrentCellDirtyStateChanged

0245: CellValidated

0246: GotFocus

0247: RowHeightInfoNeeded

0248: RowHeightInfoNeeded

0249: Invalidated

0250: ControlRemoved

0251: RowHeightInfoNeeded

0252: RowHeightInfoNeeded

0253: Invalidated

0254: CellEndEdit

0255: RowValidating

0256: RowHeightInfoNeeded

0257: RowHeightInfoNeeded

0258: Invalidated

0259: DataBindingComplete

0260: RowHeightInfoNeeded

0261: RowHeightInfoNeeded

0262: Invalidated

0263: RowValidated

0264: RowHeightInfoNeeded

0265: RowHeightInfoNeeded

0266: RowHeightInfoNeeded

0267: RowHeightInfoNeeded

0268: Invalidated

0269: RowStateChanged

0270: RowHeightInfoNeeded

0271: RowHeightInfoNeeded

0272: RowHeightInfoNeeded

0273: Invalidated

0274: RowStateChanged

0275: RowEnter

0276: CurrentCellChanged

0277: CellEnter

0278: RowHeightInfoNeeded

0279: RowHeightInfoNeeded

0280: Invalidated

0281: RowHeightInfoNeeded

0282: RowHeightInfoNeeded

0283: Invalidated

0284: RowHeightInfoNeeded

0285: RowHeightInfoNeeded

0286: RowHeightInfoNeeded

0287: Invalidated

0288: RowHeightInfoNeeded

0289: RowHeightInfoNeeded

0290: RowHeightInfoNeeded

0291: Invalidated

0292: SelectionChanged

0293: KeyUp

0294: RowHeightInfoNeeded

0295: RowErrorTextNeeded

0296: RowPrePaint

0297: RowErrorTextNeeded

0298: CellPainting

0299: CellErrorTextNeeded

0300: CellFormatting

0301: CellPainting

0302: RowErrorTextNeeded

0303: RowPostPaint

0304: RowHeightInfoNeeded

0305: RowErrorTextNeeded

0306: RowPrePaint

0307: RowErrorTextNeeded

0308: CellPainting

0309: CellErrorTextNeeded

0310: CellFormatting

0311: CellPainting

0312: RowErrorTextNeeded

0313: RowPostPaint

0314: Paint

 

Вот и все J

Высота строки в DataGridView

Высоту строк можно задавать в обработчике события DataGridView.RowHeightInfoNeeded. Например, если высота каждой второй строки должна быть равна 50 пикселей, то в обработчике пишем следующее:

 

if(e.RowIndex % 2 != 0)

    e.Height = 50;

else

    e.Height = 24;

 

else … – это обязательно; иначе высота у всех строк будет одинаковая.