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.