none
How to add comboBox in a datagrid changing the code from C# to VB.Net RRS feed

  • 問題

  • I try to add the combobox in the dataGrid. I download the source code from website ( http://msdn.microsoft.com/msdnmag/issues/03/08/DataGrids/ ).
    But I face a problem, I don't know how to change the following sub procedure 
    (private void comboBox_Leave(object sender, EventArgs e)) from C# source code to VB.Net code. 

    Please read the red font and green font. I try to change the following code from SetColumnValueAtRow(this.cm, this.iCurrentRow, s) -> Sender.SetColumnValueAtRow(me.cm, me.iCurrentRow, s). The VS2003 alerted my the following message " 'Me' is valid only within a instance method "


    //Here is a part of the source code
    using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Data;
    using System.Diagnostics;


    namespace MSDNMag.DataGridDemo
    {
     // Derive class from DataGridTextBoxColumn
     public class DataGridComboBoxColumn : DataGridTextBoxColumn
     {
      // Hosted ComboBox control
      private ComboBox comboBox;
      private CurrencyManager cm;
      private int iCurrentRow;


                                    private void comboBox_Leave(object sender, EventArgs e)
      {
    DataRowView rowView =(DataRowView)this.comboBox.SelectedItem;
    string s = (string) rowView.Row[this.comboBox.DisplayMember];
    Debug.WriteLine(String.Format("Leave: {0} {1}", this.comboBox.Text, s));

    SetColumnValueAtRow(this.cm, this.iCurrentRow, s);
    Invalidate();

    this.comboBox.Hide();
    this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler(DataGrid_Scroll);   
      }
     }
    }
     

     

    //Here is convert from C# code to VB.Net code in comboBox_leave procedure

       Private Shared Sub comboBox_Leave(ByVal sender As Object, ByVal e As EventArgs)

                Dim rowView As DataRowView = CType(sender.SelectedItem, DataRowView)
                Dim s As String = CType(rowView.Row(sender.DisplayMember), String)
                Debug.WriteLine(String.Format("Leave: {0} {1}", sender.Text, s))

                sender.SetColumnValueAtRow(me..cm, me.iCurrentRow, s)
                sender.Invalidate()

                sender.Hide()
                AddHandler sender.DataGridTableStyle.DataGrid.Scroll, New EventHandler(AddressOf DataGrid_Scroll)


            End Sub

    2006年8月7日 上午 09:10

解答

  • public class DataGridComboBoxColumn : DataGridTextBoxColumn
     {
    ...
               private void comboBox_Leave(object sender, EventArgs e)
     
             {
    DataRowView rowView =(DataRowView)this.comboBox.SelectedItem;
    string s = (string) rowView.Row[this.comboBox.DisplayMember];
    Debug.WriteLine(String.Format("Leave: {0} {1}", this.comboBox.Text, s));

    SetColumnValueAtRow(this.cm, this.iCurrentRow, s);

    check your VB Sub write in the class DataGridComboBoxColumn ?
    then you can use "Me" in Sub
    comboBox_Leave
       Private Shared Sub comboBox_Leave(ByVal sender As Object, ByVal e As EventArgs)

    2006年8月18日 上午 03:37