Winform下的Datagrid的列風格(4)—DataGridComboBoxTableViewColumn

weixin_34104341發表於2020-04-07
class  DataGridComboBoxTableViewColumn: DataGridColumnStyle 
ExpandedBlockStart.gifContractedBlock.gif    
{//與DataGridComboBoxColumn不同,顯示時使用DataTable,下拉時使用DataView
        private ComboBox myComboBox = new ComboBox ();
        
private DataView ChoiceDataViewSource;
        
private DataView TableDataViewSource;
        
private string ChoiceDisplayField;
        
private string ChoiceValueField;
        
private bool isEditing;

        
public DataGridComboBoxTableViewColumn(DataView ChoiceDataViewSource,string ChoiceDisplayField,string ChoiceValueField) : base() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.ChoiceDataViewSource =ChoiceDataViewSource;
            TableDataViewSource
=new System.Data .DataView (ChoiceDataViewSource.Table);
            
this.ChoiceDisplayField =ChoiceDisplayField;
            
this.ChoiceValueField =ChoiceValueField;
            myComboBox.DropDownStyle 
=System.Windows .Forms .ComboBoxStyle .DropDownList  ;
            myComboBox.Visible 
= false;
            myComboBox.DataSource 
=this.ChoiceDataViewSource;
            myComboBox.DisplayMember
= this.ChoiceDisplayField;
            myComboBox.ValueMember 
=this.ChoiceValueField;
        }

        
protected override void Abort(int rowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            isEditing 
= false;
            myComboBox.SelectedIndexChanged 
-= 
                
new EventHandler(ComboBoxSelectedIndexChanged );
            Invalidate();
        }


        
protected override bool Commit
            (CurrencyManager dataSource, 
int rowNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            myComboBox.Bounds 
= Rectangle.Empty;
         
            myComboBox.SelectedIndexChanged 
-= 
                
new EventHandler(ComboBoxSelectedIndexChanged );

            
if (!isEditing)
                
return true;

            isEditing 
= false;

            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                System.Object  value 
= myComboBox.SelectedValue;
                SetColumnValueAtRow(dataSource, rowNum, value);
            }
 
            
catch (Exception) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Abort(rowNum);
                
return false;
            }


            Invalidate();
            
return true;
        }


        
protected override void Edit(
            CurrencyManager source, 
            
int rowNum,
            Rectangle bounds, 
            
bool readOnly,
            
string instantText, 
            
bool cellIsVisible) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
object value =GetColumnValueAtRow(source, rowNum);
            
if (cellIsVisible) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                myComboBox.Bounds 
= new Rectangle
                    (bounds.X 
+ 2, bounds.Y + 2
                    bounds.Width 
- 4, bounds.Height - 4);
                
int i=    Find (value);
                
if (myComboBox.Items .Count >i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    myComboBox.SelectedIndex 
=i;
                }

                myComboBox.Visible 
= true;
                myComboBox.SelectedIndexChanged 
+= 
                    
new EventHandler(ComboBoxSelectedIndexChanged );
            }
 
            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                myComboBox.SelectedIndex 
=this.Find (value);
                myComboBox.Visible 
= false;
            }


            
if (myComboBox.Visible)
                DataGridTableStyle.DataGrid.Invalidate(bounds);
        }

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

            
for (int1=0;int1<int2;int1++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (TableDataViewSource[int1][this.myComboBox .ValueMember ].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, myComboBox.PreferredHeight+ 4);
        }


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


        
protected override int GetPreferredHeight(Graphics g, 
            
object value) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return myComboBox.PreferredHeight + 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.TableDataViewSource [int1][this.myComboBox .DisplayMember ].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        
{
            
base.SetDataGridInColumn(value);
            
if (myComboBox.Parent != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                myComboBox.Parent.Controls.Remove 
                    (myComboBox);
            }

            
if (value != null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                value.Controls.Add(myComboBox);
            }

        }


        
private void ComboBoxSelectedIndexChanged(object sender, EventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.isEditing = true;
            
base.ColumnStartedEditing(myComboBox);
        }

    }

轉載於:https://www.cnblogs.com/yitian/archive/2008/09/12/1290000.html

相關文章