Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
//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);
        }
Posted

You can check DataGridView.RowCount[^] or DataGridView.Rows.Count[^] before adding a new row and show a message if you already got 5...
 
Share this answer
 
Comments
Member 11657542 17-May-15 10:37am    
Thanks it works!
Sergey Alexandrovich Kryukov 17-May-15 10:44am    
Not quite. Please see my comment to "Solution 3".

Peter,

You would need to explain what events would you need to subscribe to to detect adding a row.

See also alternative Solution 2.

—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
 
Share this answer
 
C#
 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");
}

}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-May-15 10:42am    
This is if you use some button. But the new row can be added by entering data in the very last row which is the mechanism for adding rows. How are you going to limit it?
—SA
Member 11657542 17-May-15 11:01am    
when user try to enter 6th record he can't because a messagebox will generated.
Sergey Alexandrovich Kryukov 17-May-15 11:16am    
No, because it can be done without pressing your button.
—SA
Sergey Alexandrovich Kryukov 17-May-15 10:43am    
And this is not an answer. You just used Solution 1. And how you could accept your own "solution" formally?
This is the abuse.
—SA
see my code is working as I want to do, and I have taken idea from your suggestion to use "RowCount".


C#
        //button add data to dataGridView
        //insert image from pictureBox to dataGridView
        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");
            }

            }

private void btn_Save_Click(object sender, EventArgs e)
        {
            int numberOfRows = dataGridView1.Rows.Count;
            if (numberOfRows == 5)
            {

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    string insert_sql = "INSERT INTO Input(UserID, UserName, PassImage) VALUES ('" + col1 + "','" + col2 + "','" + col3 + "')";
                    this.getcom(insert_sql);
                }

                MessageBox.Show("Record Added");

            }
            else
            {
                MessageBox.Show("Please Enter 5 Images");
            }

         }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900