2011-11-28 6 views
0

誰もこのコードで何が間違っているのか教えてください。私は下矢印キーを押したときに、私は現在の行はnull例外を取得しています以下の現在の行は、DataGridviewでnullを返します。C#

BindingSource source = new BindingSource(); 
source.DataSource = ies.tblProducts; 
General.autoSuggest(this, txtProduct,source,new int[]{0}); 

のように別の形でこのメソッドを呼び出すときに、この私は、現在の行

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace App 
{ 
    public static class General 
    { 
     private static BindingSource source; 
     private static bool typing = false; 
     private static DataGridView dgSuggest; 
     public static int productID; 
     private static ComponentFactory.Krypton.Toolkit.KryptonTextBox txtBox; 
     /// <summary> 
     /// To show the auto suggest 
     /// </summary> 
     /// <param name="parent">Windows.Form</param> 
     /// <param name="tBox">krypton TextBox</param> 
     /// <param name="dataSource">Datasource for the Suggestion</param> 
     /// <param name="indexes">Indexes to hide in suggestion</param> 
     public static void autoSuggest(Form parent,ComponentFactory.Krypton.Toolkit.KryptonTextBox tBox,BindingSource dataSource,int[] indexes) 
     { 
      source = dataSource; 
      txtBox = tBox; 
      dgSuggest = new DataGridView(); 
      parent.Controls.Add(dgSuggest); 
      dgSuggest.BringToFront(); 
      dgSuggest.AllowUserToAddRows = false; 
      dgSuggest.AllowUserToDeleteRows = false; 
      dgSuggest.Location = new System.Drawing.Point(txtBox.Location.X,txtBox.Location.Y + txtBox.Height); 
      dgSuggest.Width = txtBox.Width; 
      dgSuggest.ReadOnly = true; 
      dgSuggest.RowHeadersVisible = false; 
      dgSuggest.DataSource = source; 
      dgSuggest.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 

      dgSuggest.Columns[0].Visible = false; 
      dgSuggest.Columns[2].Visible = false; 
      dgSuggest.Columns[3].Visible = false; 
      dgSuggest.Columns[4].Visible = false; 
      dgSuggest.Columns[5].Visible = false; 
      dgSuggest.Columns[6].Visible = false; 
      dgSuggest.Columns[7].Visible = false; 
      dgSuggest.Columns[8].Visible = false; 
      dgSuggest.Columns[9].Visible = false; 

      dgSuggest.Hide(); 
      dgSuggest.Columns[1].Width = txtBox.Width - 20; 
      txtBox.KeyDown += new KeyEventHandler(txtBox_KeyDown); 
      txtBox.KeyPress += new KeyPressEventHandler(txtBox_KeyPress); 
      txtBox.TextChanged += new EventHandler(txtBox_TextChanged); 
     } 

     private static void txtBox_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (dgSuggest.Visible == false) 
      { 
       dgSuggest.Visible = true; 
      } 

      if (e.KeyCode == Keys.Down) 
      { 
       int rowIndex = 0; 
       if (dgSuggest.Rows.Count > 0) 
       { 
        if (dgSuggest.CurrentRow.Index < dgSuggest.Rows.Count - 1) 
        { 
         rowIndex = dgSuggest.CurrentRow.Index + 1; 
        } 

        dgSuggest.ClearSelection(); 
        dgSuggest.CurrentCell = dgSuggest.Rows[rowIndex].Cells[1]; 
        dgSuggest.Rows[rowIndex].Selected = true; 
       } 
      } 
      else if (e.KeyCode == Keys.Up) 
      { 
       int rowIndex = 0; 
       if (dgSuggest.Rows.Count > 0) 
       { 
        if (dgSuggest.CurrentRow.Index > 0) 
        { 
         rowIndex = dgSuggest.CurrentRow.Index - 1; 
        } 
        else 
        { 
         rowIndex = dgSuggest.Rows.Count - 1; 
        } 

        dgSuggest.ClearSelection(); 
        dgSuggest.CurrentCell = dgSuggest.Rows[rowIndex].Cells[1]; 
        dgSuggest.Rows[rowIndex].Selected = true; 
       } 
      } 
      else if (e.KeyCode == Keys.Enter) 
      { 
       DataGridViewRow row = dgSuggest.CurrentRow; 
       productID = Convert.ToInt32(row.Cells[0].Value); 
       txtBox.Text = Convert.ToString(row.Cells[1].Value); 
       dgSuggest.Hide(); 
      } 
      else if (e.KeyCode == Keys.Back) 
      { 
       if (txtBox.Text != "") 
       { 
        txtBox.Text = txtBox.Text.Substring(0, txtBox.Text.Length - 1); 
       } 
      } 
      else if (e.KeyCode == Keys.Delete) 
      { 
       txtBox.Text = ""; 
      } 

      e.Handled = true; 
     } 


     private static void txtBox_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (char.IsControl(e.KeyChar) == false) 
      { 
       txtBox.Text = txtBox.Text + Convert.ToString(e.KeyChar); 
       typing = true; 
      } 

      if(e.KeyChar == Convert.ToChar(Keys.Back)) 
      { 
       typing = true; 
      } 
      e.Handled = true; 
     } 

     private static void txtBox_TextChanged(object sender, EventArgs e) 
     { 
      if (typing) 
      { 
       if (txtBox.Text == "") 
       { 
        source.Filter = ""; 
       } 
       else 
       { 
        MessageBox.Show(source.Filter); 
        source.Filter = "Name LIKE '"+txtBox.Text+"%'"; 
       } 
      } 
      txtBox.SelectionStart = 0; 
      txtBox.SelectionLength = txtBox.Text.Length; 
     } 
    } 
} 

にヌル例外を取得していますしようとすると、キー

答えて

0

DataGridviewを初めて開いたときに現在の行が選択されていますか?あなたのkeydownセクションで、現在の行がヌルでないことを確認する必要があります:

if (dgSuggest.CurrentRow != null && 
    dgSuggest.CurrentRow.Index < dgSuggest.Rows.Count - 1) 
{ 
    rowIndex = dgSuggest.CurrentRow.Index + 1; 
} 

他のセクションにも同じことが言えます。

EDIT:下記の追加情報

出典:Hide()は使用しないでください。代わりに、DataGridViewのVisibleプロパティを変更することをお勧めします。 Hide()を呼び出すと、CurrentRowがnullに設定され、質問のコードにエラーが発生します。

+0

もし私が非表示にすると、列が正しく動作していない場合、エラーが発生します。 –

+1

これが見つかりました:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.controls.datagridview.hide.aspx Hide()は使用しないでください。代わりに.Visible = Falseを使用しないでください。 –

+1

@MubarakAli自分でテストしたところで、Hide()を呼び出すと、CurrentRowがnullに設定されます。だからあなたは何か他のものを使わなければならない(例えば.Visible = false;) –

関連する問題