DataGridView 中的文本框列

发布于 2024-12-07 07:43:50 字数 116 浏览 0 评论 0原文

我在网格内添加了 TextBox 控件:我希望我的 DataGridView TextBox 列能够保存没有任何小数值的数字。我该怎么做呢?

I have added TextBox control inside the grid: I want my DataGridView TextBox column to hold numbers without any decimal values. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一世旳自豪 2024-12-14 07:43:50

来自:http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/919b059c-dba9-40d2-bac7-608a9b120336

您可以处理 DataGridView.EditingControlShowing 事件来进行强制转换
在要编辑的列中编辑时将编辑控件移至TextBox
限制输入,并将 KeyPress 事件附加到 TextBox,在
KeyPress事件处理函数,我们可以调用char.IsNumber()
限制键盘输入的方法,如下所示:

private void Form1_Load(object sender, EventArgs e)
{
   DataTable dt = new DataTable();
   dt.Columns.Add("c1", typeof(int));
   dt.Columns.Add("c2");
   for (int j = 0; j < 10; j++)
   {
      dt.Rows.Add(j, "aaa" + j.ToString());
   }

   this.dataGridView1.DataSource = dt;
   this.dataGridView1.EditingControlShowing +=
      new DataGridViewEditingControlShowingEventHandler(
         dataGridView1_EditingControlShowing);
}

private bool IsHandleAdded;

void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
   if (!IsHandleAdded &&
       this.dataGridView1.CurrentCell.ColumnIndex == 0)
   {
      TextBox tx = e.Control as TextBox;
      if (tx != null)
      {
         tx.KeyPress += new KeyPressEventHandler(tx_KeyPress);
         this.IsHandleAdded = true;
      }
   }
}

void tx_KeyPress(object sender, KeyPressEventArgs e)
{
   if (!(char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
   {
      e.Handled = true;
   }
}

From: http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/919b059c-dba9-40d2-bac7-608a9b120336

You can handle the DataGridView.EditingControlShowing event to cast
the editing control to TextBox when editing in the column you want to
restrict input on, and attach KeyPress event to the TextBox, in the
KeyPress event handler function, we can call the char.IsNumber()
method to restrict the key board input, something like this:

private void Form1_Load(object sender, EventArgs e)
{
   DataTable dt = new DataTable();
   dt.Columns.Add("c1", typeof(int));
   dt.Columns.Add("c2");
   for (int j = 0; j < 10; j++)
   {
      dt.Rows.Add(j, "aaa" + j.ToString());
   }

   this.dataGridView1.DataSource = dt;
   this.dataGridView1.EditingControlShowing +=
      new DataGridViewEditingControlShowingEventHandler(
         dataGridView1_EditingControlShowing);
}

private bool IsHandleAdded;

void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
   if (!IsHandleAdded &&
       this.dataGridView1.CurrentCell.ColumnIndex == 0)
   {
      TextBox tx = e.Control as TextBox;
      if (tx != null)
      {
         tx.KeyPress += new KeyPressEventHandler(tx_KeyPress);
         this.IsHandleAdded = true;
      }
   }
}

void tx_KeyPress(object sender, KeyPressEventArgs e)
{
   if (!(char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
   {
      e.Handled = true;
   }
}
请恋爱 2024-12-14 07:43:50
public Form1()
{
   InitializeComponent();
   dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
}

private void dataGridView1_EditingControlShowing(object sender , DataGridViewEditingControlShowingEventArgs e)
{
   // Convert the editing control to a TextBox to register for KeyPress event
   TextBox txt_edit = e.Control as TextBox;
   if(txt_edit != null)
   {
      // Remove any existing handler
      txt_edit.KeyPress -= TextBoxKeyPressed;
      // Add the new handler
      txt_edit.KeyPress += TextBoxKeyPressed;
   }
}

private void TextBoxKeyPressed(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
   // Test for numeric value or backspace in first column
   // Change this to whatever column you only want digits for
   if (dataGridView1.CurrentCell.ColumnIndex == 0)
   {
      // If its a numeric or backspace, display it.  Not a numeric, ignore it
      e.Handled = (!Char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Back));
   }
}
public Form1()
{
   InitializeComponent();
   dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
}

private void dataGridView1_EditingControlShowing(object sender , DataGridViewEditingControlShowingEventArgs e)
{
   // Convert the editing control to a TextBox to register for KeyPress event
   TextBox txt_edit = e.Control as TextBox;
   if(txt_edit != null)
   {
      // Remove any existing handler
      txt_edit.KeyPress -= TextBoxKeyPressed;
      // Add the new handler
      txt_edit.KeyPress += TextBoxKeyPressed;
   }
}

private void TextBoxKeyPressed(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
   // Test for numeric value or backspace in first column
   // Change this to whatever column you only want digits for
   if (dataGridView1.CurrentCell.ColumnIndex == 0)
   {
      // If its a numeric or backspace, display it.  Not a numeric, ignore it
      e.Handled = (!Char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Back));
   }
}
趴在窗边数星星i 2024-12-14 07:43:50

试试这个:

Convert.ToInt32(textBox1.Text)

Try this:

Convert.ToInt32(textBox1.Text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文