如何使用C#限制用户在DataGridView中输入5个输入 [英] How do I restrict user to enter exactly 5 input in DataGridView using C#

查看:59
本文介绍了如何使用C#限制用户在DataGridView中输入5个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时从用户那里输入正好5条记录并在datagridview中显示它可以让任何人知道如何限制用户吗?尝试输入第6条记录时会生成一条消息。以下是我在datagridview中输入记录的代码。



I want to take input of exactly 5 records from user at runtime and show it in datagridview can anybody know how to restrict user for this? a message will generated on try for entering 6th record. following is my code where I entered records in datagridview.

//button add data to dataGridView
        //insert image from pictureBox to dataGridView
        private void btn_Add_Click(object sender, EventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
            byte[] img = ms.ToArray();
            dataGridView1.Rows.Add(txt_UserID.Text, txt_Name.Text, img);
        }

推荐答案

您可以查看DataGridView.RowCount [ ^ ]或 DataGridView.Rows.Count [ ^ ]之前添加一个新行并显示一条消息,如果你已经有5 ...
You can check DataGridView.RowCount[^] or DataGridView.Rows.Count[^] before adding a new row and show a message if you already got 5...


除了解决方案1,你可以预先创建 DataGridView 正好有5行的实例,可以在某些单元格中保留为空,需要用户输入。



这是布尔专业版perty允许用户添加一行: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.allowusertoaddrows%28v=vs.110%29.aspx [ ^ ]。



默认情况下是真的,您需要将其值更改为false。



-SA
Alternatively to Solution 1, you can pre-create DataGridView instance with exactly 5 rows, which can be left empty in some cell, requiring user's input in it.

This is the Boolean property which allows the user to add a row: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.allowusertoaddrows%28v=vs.110%29.aspx[^].

It is true by default, and you would need to change its value to false.

—SA


 private void btn_Add_Click(object sender, EventArgs e)
{
int numberOfRows = dataGridView1.Rows.Count;
if (numberOfRows < 5)
{
    MemoryStream ms = new MemoryStream();
    pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
    byte[] img = ms.ToArray();
    dataGridView1.Rows.Add(txt_UserID.Text, txt_Name.Text, img);
}
    else
{
    MessageBox.Show("Please insert Only 5 Images");
}

}


这篇关于如何使用C#限制用户在DataGridView中输入5个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆