Problem Defination : On button click we want to add textbox vales and combo box values into the datagridview and after that when we click on submit button it will have to save into database. the following figure shows the details of it.
In the above figure we use a DataGridview when we click on ADD Medicines Button values from Medicine Name combo box, Batch No text box , medicine Quantity and Rate will have to add into the DataGridView and when we click on the Save button it will save into the Database.
Double Click on the Add Medicines Button and write the following code into it..
int z, q;
private void btnAddMedicine_Click(object sender, EventArgs e)
{
try
{
if (q == 0)
{
dataGridView1.Columns.Add("MedicineID", "MedicineID");
dataGridView1.Columns.Add("MedicineName", "MedicineName");
dataGridView1.Columns.Add("BatchNumber", "BatchNumber");
dataGridView1.Columns.Add("Quantity", "Quantity");
dataGridView1.Columns.Add("Rate", "Rate");
q++;
}
dataGridView1.Rows.Add();
dataGridView1.Rows[z].Cells[0].Value = Convert.ToInt32(cmbMedicineId.SelectedValue);
dataGridView1.Rows[z].Cells[1].Value = cmbMedicineId.Text;
dataGridView1.Rows[z].Cells[2].Value = cmbBatchNo.Text;
dataGridView1.Rows[z].Cells[3].Value = Convert.ToInt32(txtMedicineQuantity.Text);
dataGridView1.Rows[z].Cells[4].Value = Convert.ToDouble(txtRate.Text);
z++;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Firstly , we have to add Columns MedicineID,MedicineName,BatchNumber,Quantity,Rate by following code.
dataGridView1.Columns.Add("MedicineID", "MedicineID");
dataGridView1.Columns.Add("MedicineName", "MedicineName");
dataGridView1.Columns.Add("BatchNumber", "BatchNumber");
dataGridView1.Columns.Add("Quantity", "Quantity");
dataGridView1.Columns.Add("Rate", "Rate");
Now we have to add rows so following code is used for it.
dataGridView1.Rows.Add();
dataGridView1.Rows[z].Cells[0].Value = Convert.ToInt32(cmbMedicineId.SelectedValue);
dataGridView1.Rows[z].Cells[1].Value = cmbMedicineId.Text;
dataGridView1.Rows[z].Cells[2].Value = cmbBatchNo.Text;
dataGridView1.Rows[z].Cells[3].Value = Convert.ToInt32(txtMedicineQuantity.Text);
dataGridView1.Rows[z].Cells[4].Value = Convert.ToDouble(txtRate.Text);
z++;
No comments:
Post a Comment