Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There are 4 textboxes (txt1, txt2, txt3 & txt4). These 4 textboxes contains values like

txt1=accNo | txt2=Publisher   |  txt3=Price | txt4=qty
500        | Geeta Publisher  |  350        | 5


I want to pass my textbox values into Datagridview as bellow shown.

AccNo | Publisher          | Price 
500   | Geeta Publisher    | 350 
501   | Geeta Publisher    | 350
502   | Geeta Publisher    | 350 
503   | Geeta Publisher    | 350


AccNo will be added into DataGridView 5 times (mentioned in qty) and auto increase by 1.

Please help....

What I have tried:

...........................................
Posted
Updated 24-Nov-17 18:33pm

1 solution

try

private void button1_Click(object sender, EventArgs e)
        {
            int accountNo;            
            double price;
            int quantity;
            string publisher = txtPublisher.Text.Trim();
            if(!int.TryParse(txtAccNo.Text.Trim(),out accountNo))
            {
                MessageBox.Show("Please enter valid Account Number");
                return;
            }
            if (!int.TryParse(txtqty.Text.Trim(), out quantity))
            {
                MessageBox.Show("Please enter valid quantity");
                return;
            }
            if (!double.TryParse(txtPrice.Text.Trim(), out price))
            {
                MessageBox.Show("Please enter valid price");
                return;
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("AccNo", typeof(int));
            dt.Columns.Add("Publisher");
            dt.Columns.Add("Price", typeof(double));
            for (int i = 0; i < quantity; i++)
            {
                DataRow row = dt.NewRow();
                row["AccNo"] =   accountNo + i;
                row["Publisher"] = publisher;
                row["Price"] = price;
                dt.Rows.Add(row);

            }
            dataGridView1.DataSource = dt;
        }


Please use a proper naming convention to the controls which will help you to find the controls in the code behind easily.
 
Share this answer
 
Comments
Laxmidhar tatwa technologies 27-Nov-17 7:27am    
your code looks powerfull

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