My code for button
C#
Button btn = (Button)sender;
var itemAndPrice = btn.Text;
int sno = dataGridView1.Rows.Count + 1;
string nazivartikla = itemAndPrice.Split('-')[0].ToString();
double cijena = Convert.ToDouble(itemAndPrice.Split('-')[1].ToString());
string exists = "";
NazivArtiklaTextBox.Text = nazivartikla;
Provjera_stanja();
Sifra_i_Podgrupa_robe();
foreach (DataGridViewRow g1 in dataGridView1.Rows)
{
if (g1.Cells[2].Value.ToString().Trim() == itemAndPrice.Split('-')[0].ToString().Trim())
{
exists = "y";
g1.Cells[3].Value = Convert.ToInt32(g1.Cells[3].Value) + 1;
g1.Cells[5].Value = Convert.ToDouble(g1.Cells[3].Value) * Convert.ToDouble(g1.Cells[4].Value);
}
}
if (exists != "y")
{
dataGridView1.Rows.Add(sno, SifraTextBox.Text, nazivartikla, 1, cijena, cijena);
}
double[] columnData = new double[dataGridView1.Rows.Count];
columnData = (from DataGridViewRow row in dataGridView1.Rows
where row.Cells["kolicina"].FormattedValue.ToString() != string.Empty
select Convert.ToDouble(row.Cells["kolicina"].FormattedValue)).ToArray();
kolicinalabel.Text = columnData.Sum().ToString("0.00");
double[] columnData2 = new double[dataGridView1.Rows.Count];
columnData2 = (from DataGridViewRow row in dataGridView1.Rows
where row.Cells["ukupno"].FormattedValue.ToString() != string.Empty
select Convert.ToDouble(row.Cells["ukupno"].FormattedValue)).ToArray();
ukupnolabel.Text = columnData2.Sum().ToString("0.00");
UkupnobezpdvLabel.Text = columnData2.Sum().ToString("0.00");
label15.Text = columnData2.Sum().ToString("0.00");
double ukupno;
if (!double.TryParse(ukupnolabel.Text, out ukupno))
{
// ... report problem to user ...
return;
}
double lab3;
if (!double.TryParse(label3.Text, out lab3))
{
//... report problem to user ...
return;
}
label4.Text = (ukupno * lab3 / 100.0).ToString("0.00");
dataGridView1.Refresh();my code for control button in line 123 call button click function but not work
C#
private void TopliNapitciButton_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
string conString = ConnectionClass.PullData(labelgodina.Text);
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("SELECT naziv, FORMAT(cijena_sa_porezom, 'N2') AS cijena, data FROM dbo.roba_usluge where grupa_artikala='Napitci' and status='Aktivan'", con);
// SqlCommand cmd = new SqlCommand("SELECT naziv+' - '+CAST(FORMAT(cijena_sa_porezom, 'N2') AS varchar), data FROM dbo.roba_usluge where grupa_artikala='Napitci' and status='Aktivan'", con);
var da = new SqlDataAdapter(cmd);
var ItemTable = new DataTable();
da.Fill(ItemTable);
con.Open();
Int32 count = ItemTable.Rows.Count;
con.Close();
int top = 270;
int left = 8;
for (int i = 0; i
{
// Access the captured currentIndex instead of i
string itemName = ItemTable.Rows[currentIndex]["naziv"].ToString();
string itemPrice = ItemTable.Rows[currentIndex]["cijena"].ToString();
// Insert into DataGridView
int sno = dataGridView1.Rows.Count + 1;
string nazivartikla = itemName;
double cijena = Convert.ToDouble(itemPrice.Replace(" KM", ""));
string exists = "";
NazivArtiklaTextBox.Text = nazivartikla;
Sifra_i_Podgrupa_robe();
Provjera_stanja();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[2].Value != null && row.Cells[2].Value.ToString().Trim() == nazivartikla)
{
exists = "y";
row.Cells[3].Value = Convert.ToInt32(row.Cells[3].Value) + 1;
row.Cells[5].Value = Convert.ToDouble(row.Cells[3].Value) * Convert.ToDouble(row.Cells[4].Value);
break; // Exit loop once found
}
}
if (exists != "y")
{
dataGridView1.Rows.Add(sno, SifraTextBox.Text, nazivartikla, 1, cijena, cijena);
}
button.Click += new EventHandler(this.btn_Click);
dataGridView1.Refresh();
};
if ((i + 1) % 5 == 0)
{
left = 8;
top += button.Height + 2;
}
else
{
left += button.Width + 2;
}
}
}
Tahir AnsariRank #366 · 6.8K reputationPosted Apr 9, 2024, 4:46 AM EDT
you're attempting to assign a click event handler to the
pictureBox, but you actually want it for thebutton. You should move the event assignment outside thepictureBoxclick event handler and assign it directly to thebutton.button.Click += (s, ev) => { // Access the captured currentIndex instead of i string itemName = ItemTable.Rows[currentIndex]["naziv"].ToString(); string itemPrice = ItemTable.Rows[currentIndex]["cijena"].ToString(); // Insert into DataGridView int sno = dataGridView1.Rows.Count + 1; string nazivartikla = itemName; double cijena = Convert.ToDouble(itemPrice.Replace(" KM", "")); string exists = ""; NazivArtiklaTextBox.Text = nazivartikla; Sifra_i_Podgrupa_robe(); Provjera_stanja(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[2].Value != null && row.Cells[2].Value.ToString().Trim() == nazivartikla) { exists = "y"; row.Cells[3].Value = Convert.ToInt32(row.Cells[3].Value) + 1; row.Cells[5].Value = Convert.ToDouble(row.Cells[3].Value) * Convert.ToDouble(row.Cells[4].Value); break; // Exit loop once found } } if (exists != "y") { dataGridView1.Rows.Add(sno, SifraTextBox.Text, nazivartikla, 1, cijena, cijena); } // Call the button click event handler directly btn_Click(s, ev); dataGridView1.Refresh(); }; // Add button to panel panel1.Controls.Add(button);Goran BibicRank #993 · 2K reputationPosted Apr 9, 2024, 4:56 AM EDT
TEsted, not work
On click nothing happend