Datagrid column style in Winform (2) -DataGridTextBindColumn (ZHUAN)

class  DataGridTextBindColumn:DataGridColumnStyle
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private DataView ChoiceDataViewSource;
        
private string ChoiceDisplayField;
        
private string ChoiceValueField;
        
public DataGridTextBindColumn(DataView ChoiceDataViewSource,string ChoiceDisplayField,string ChoiceValueField) : base() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.ChoiceDataViewSource =ChoiceDataViewSource;
            
this.ChoiceDisplayField =ChoiceDisplayField;
            
this.ChoiceValueField =ChoiceValueField;
        }

        
protected override void Abort(int rowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Invalidate();
        }


        
protected override bool Commit
            (CurrencyManager dataSource, 
int rowNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Invalidate();
            
return true;
        }


        
protected override void Edit(
            CurrencyManager source, 
            
int rowNum,
            Rectangle bounds, 
            
bool readOnly,
            
string instantText, 
            
bool cellIsVisible) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

        }

        
protected int Find(System.Object value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int int1;
            
int int2;
            
            int2
=ChoiceDataViewSource.Count ;
            
if (int2<=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return -1;
            }

            
for (int1=0;int1<int2;int1++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (ChoiceDataViewSource[int1][ChoiceValueField ].ToString ().Trim ()==value.ToString ().Trim ())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return int1;
                }

            }

            
return -1;
        }


        
protected override Size GetPreferredSize(
            Graphics g, 
            
object value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return new Size(100,  4);
        }


        
protected override int GetMinimumHeight() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return  4;
        }


        
protected override int GetPreferredHeight(Graphics g, 
            
object value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return  4;
        }


        
protected override void Paint(Graphics g, 
            Rectangle bounds, 
            CurrencyManager source, 
            
int rowNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Paint(g, bounds, source, rowNum, 
false);
        }

        
protected override void Paint(
            Graphics g, 
            Rectangle bounds,
            CurrencyManager source, 
            
int rowNum,
            
bool alignToRight) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Paint(
                g,bounds, 
                source, 
                rowNum, 
                Brushes.Red, 
                Brushes.Blue, 
                alignToRight);
        }

        
protected override void Paint(
            Graphics g, 
            Rectangle bounds,
            CurrencyManager source, 
            
int rowNum,
            Brush backBrush, 
            Brush foreBrush,
            
bool alignToRight) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
object value=GetColumnValueAtRow(source, rowNum);
            
int int1=Find (value);
            
string display="";
            
if (int1>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                display
=this.ChoiceDataViewSource [int1][ChoiceDisplayField].ToString ();
            }

            Rectangle rect 
= bounds;
            g.FillRectangle(backBrush,rect);
            rect.Offset(
02);
            rect.Height 
-= 2;
            g.DrawString(display, 
                
this.DataGridTableStyle.DataGrid.Font, 
                foreBrush, rect);
        }


        
protected override void SetDataGridInColumn(DataGrid value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

        }


    }
        Differs from DataGridComboBoxColumn is to show only the name of the class the student belongs, but you can not change the student belongs to a class. In general, the datagrid to this DataGridTextBindColumn columns readonly to true.

Reproduced in: https: //www.cnblogs.com/yitian/archive/2008/09/12/1289996.html

Guess you like

Origin blog.csdn.net/weixin_34143774/article/details/93710321