0% found this document useful (0 votes)
119 views8 pages

C# Recepture za Rad s Podacima

The document provides code examples for working with datatables, comboboxes, datagridviews, and other common controls in C# and .NET. It includes examples of populating controls from SQL databases using datatables, dataadapters, and datareaders. Methods are demonstrated for adding, editing, deleting rows from datatables and datagridviews. The document also contains examples of printing reports, handling button clicks, and using common dialogs.

Uploaded by

Dragan Karanovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views8 pages

C# Recepture za Rad s Podacima

The document provides code examples for working with datatables, comboboxes, datagridviews, and other common controls in C# and .NET. It includes examples of populating controls from SQL databases using datatables, dataadapters, and datareaders. Methods are demonstrated for adding, editing, deleting rows from datatables and datagridviews. The document also contains examples of printing reports, handling button clicks, and using common dialogs.

Uploaded by

Dragan Karanovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C# MOJE RECEPTURE

[Link]
DataTable Dtprimka=new DataTable(); //kreiranje
[Link]("artikl_Id");// dodavanje kolumne
[Link]("1", "jedan");//dodavanje reda

[Link]
[Link] = Dtprimka;//
[Link] = "Artikl";
[Link] = "Kolicina";

DATATABLE & DATAREADER& COMBOBOX


VARIJANTA 1
[Link] = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Blagajna\Blagajna\[Link];Integrated
Security=True;Connect Timeout=30;User Instance=True";
[Link] = conn;
[Link]();

[Link] = "Select arikl_id,naziv, cijena,mjera from artikli";


SqlDataAdapter adapter = new SqlDataAdapter(command);
[Link](dtCbo);
// u dtcbo e ui svi lanovi iz select naredbe i kolumne e poprimiti imena
po lanovima iz select [Link] ne smiju biti dodane kolumne!!!, tj
datatable mora biti samo kreirana
int a = 0;
SqlDataReader dr = [Link]();
[Link] = dtCbo;
[Link] = "arikl_id";
[Link] = "naziv";

VARIJANTA 2
[Link]("id");
[Link]("naziv");
[Link]("cijena");
[Link]("mjera");
[Link] = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Blagajna\Blagajna\[Link];Integrated
Security=True;Connect Timeout=30;User Instance=True";
[Link] = conn;
[Link]();

[Link] = "Select arikl_id,naziv, cijena,mjera from artikli";


SqlDataAdapter adapter = new SqlDataAdapter(command);
int a = 0;
SqlDataReader dr = [Link]();
while ([Link]())
{
// datatable se puni kroz while [Link] moraju biti dodane kolumne!!!
int id = dr.GetInt32(0);
string naziv = [Link](1);
decimal cijena = [Link](2);
string mjera = [Link](3);
[Link](id, naziv, cijena, mjera);
}
[Link] = dtCbo;
[Link] = "id";
[Link] = "naziv";

DATATABLE & DATAREADER & FOR PETLJA


for (int y = 0; y < [Link]; y++)
{
decimal xsubTotal = [Link]([Link][y]["nv"]);
xTotal = xTotal + xsubTotal;
}

DATATABLE & DATADAPTER


[Link] = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Blagajna\Blagajna\[Link];Integrated
Security=True;Connect Timeout=30;User Instance=True";
[Link] = conn;
[Link]();
string query = "Select kategorija_id,kat_ime,kat_opis,pdv,pnp,boja from
kategorija";
DataTable dt = new DataTable();
[Link](data);

[Link] = data;
[Link] = "kategorija_id";
[Link] = "kat_ime";
[Link]();

DATATABLE & FOREACH PETLJA


int a = 0;
foreach (DataRow row in [Link])
{
//[Link]([Link]());
string curr = [Link][a]["naziv"].ToString();
if (curr == [Link])
{
[Link] = [Link][a]["cijena"].ToString();
[Link] = [Link][a]["mjera"].ToString();
broj = [Link][a]["id"].ToString();
//[Link](broj);
artiklID = broj;
}
a++;
}

BRISANJE REDA U DT
[Link][row].Delete();

DODAVANJE REDA U DT IZ DRUGE FORME


var drugaForma = [Link]<Form1>().Single();
//[Link] = [Link];
[Link]("3","tri");

TEXTBOX
foreach (TextBox tb in [Link]<TextBox>())
[Link] = "";
[Link] = [Link]();
[Link]([Link], 0); // postavlja mi na kraju izraza u textboxu

TYPE
pdvRel = [Link]([Link]) / 100;

IF LOGIKI IZRAZ
if ([Link] == "" || [Link] == "0" || [Link] == "0,")
{
znak ||=altgr +w
}
if ([Link] == "txtKolicina" && [Link] == "Poznata cijena")

{
}
STRING
[Link] = [Link]("N2");
zarezima
String proba = [Link];

// daje novni izraz sa tokama i

string novi = [Link](".", "").Trim();


int duz = [Link] - 1;
string zarez = [Link](duz, 1);

KEY PRESS METODA


private void txtKolicina_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox t = (TextBox)sender;
if ([Link](",") && [Link]() == ",")
{
[Link] = [Link]() == ","; // prekida metodu ako je
pritisnut zarez
}
else
{
[Link] = ![Link]([Link]) && [Link]() != "," &&
[Link] != 8;
}
}

DATUM
string datum = [Link]();

SQLCOMMAND PARAMETER
[Link] = "Insert into ura(dobavljac_id,) values ( @dobavljac_)";
[Link]("@dobavljac_id", [Link]);
[Link]["@dobavljac_id"].Value = [Link];
[Link]();
FOCUS NA ENTER
private void txtCijena_KeyDown(object sender, KeyEventArgs e)
{
if ([Link] == 13)
{
[Link]();
}
}

POZIVANJE METODE U DRUGOJ FORMI


var drugaForma = [Link]<Form1>().Single();
[Link] ("gile");

DATAGRID VIEW
dataGridView2_CellContentClick
int coll = [Link];
int row = [Link];
if (coll == 2)
{
kol = kol + 1;
[Link][row].Cells["Column4"].Value = [Link]();

}
4

DATAGRIDVIEW DODAVANJE REDA


[Link]();
[Link][y].Cells["Column1"].Value
racun_stavka.Rows[y]["artikl"];
[Link][y].Cells["Column2"].Value
racun_stavka.Rows[y]["cijena"];
[Link][y].Cells["Column4"].Value
racun_stavka.Rows[y]["kolicina"];
[Link][y].Cells["Column6"].Value
racun_stavka.Rows[y]["subTotal"];

=
=
=
=

DATAGRIDVIEW IZMJENA REDA


[Link][i].Cells["Column4"].Value = novaKolicina;
[Link][i].Cells["Column6"].Value = subTotal;

brisanje reda u DGW


[Link](row);

CREATE BUTTON PROGRAMATICLY


[Link] = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Blagajna\Blagajna\[Link];Integrated
Security=True;Connect Timeout=30;User Instance=True";
[Link] = conn;
[Link] = "Select arikl_id,naziv, opis,img from artikli";
SqlDataAdapter adapter = new SqlDataAdapter(command);
[Link](data);
int a = 0;
FlowLayoutPanel flp = new FlowLayoutPanel();
[Link] = [Link];
foreach (DataRow row in [Link])
{
string curr = [Link][a]["naziv"].ToString();
int r = (int)[Link][a]["arikl_id"];
string img = [Link][a]["opis"].ToString();
byte[] img_arr1 = (byte[])[Link][a]["img"];
.
Button
[Link]
[Link]
[Link]

b
=
=
=

= new Button();
curr;
new Size(126, 50);
curr;

if (img == "")
{
}
else
{
MemoryStream ms1 = new MemoryStream(img_arr1);
[Link](0, [Link]);

[Link] =[Link](ms1);
[Link] = "";
}
[Link] = r;
[Link] += new EventHandler(addStavka);
[Link] += new MouseEventHandler(b_MouseDown);
[Link] += new MouseEventHandler(b_MouseMove);
[Link](b);
//[Link]([Link]());
a++;
}
[Link](flp);
//[Link]();
[Link]();
[Link]();

}
PRINT DILALOG
PrintDialog printdialog = new PrintDialog();
PrintDocument printdocument = new PrintDocument();
PrintPreviewDialog print = new PrintPreviewDialog();
[Link] = printdocument;
[Link] = printdocument;
[Link] += new PrintPageEventHandler(Print);
DialogResult result = [Link]();
if(result==[Link])
{
[Link]();
}

PRINTANJE
//[Link]("P");
//throw new NotImplementedException();
Graphics graphic = [Link];
Font font = new Font("Courier New",12 );
int startX=10;
int startY=10;
int ofset = 100;
int razmak = 0;
[Link]("CAFE BAR GILERA", new Font("Courier New", 18), new
SolidBrush([Link]), startX, startY);
startY = startY+ (int)FontHeight + 8;
[Link]("Gilenium d.o.o.", new Font("Courier New", 12), new
SolidBrush([Link]), startX, startY);
startY = startY + (int)FontHeight + 5;
[Link]("Karela Zahradnika 22, Zagreb", new Font("Courier New",
12), new SolidBrush([Link]), startX, startY);
startY = startY + (int)FontHeight + 5;
[Link]("OIB: 12345678912", new Font("Courier New", 12), new
SolidBrush([Link]), startX, startY );
startY = startY + (int)FontHeight + 5;

[Link]("OIB: ", new Font("Courier New", 12), new


SolidBrush([Link]), startX, startY );
[Link]("12345678912 ", new Font("Courier New", 12), new
SolidBrush([Link]), startX+50, startY);
startY = startY + (int)FontHeight + 10;
for (int i = 0; i < racun_stavka.[Link]; i++)
{
string proizvod =
racun_stavka.Rows[i]["artikl"].ToString().PadRight(20);
string cijena =
racun_stavka.Rows[i]["cijena"].ToString().PadRight(8);
string kolicina =
racun_stavka.Rows[i]["kolicina"].ToString().PadRight(5);
string subtotal = racun_stavka.Rows[i]["subtotal"].ToString();
string stavka = proizvod + cijena + kolicina + subtotal;
[Link](stavka, new Font("Courier New", 10), new
SolidBrush([Link]), startX, startY );
ofset = ofset+(int)FontHeight + 5;
startY = startY + (int)FontHeight + 5;
}
ofset = ofset + 20;
startY = startY + (int)FontHeight + 10;
[Link]("ukupno".PadRight(25) + [Link], new
Font("Courier New", 12), new SolidBrush([Link]), startX, startY );

COLOR DIALOG
ColorDialog colorDlg = new ColorDialog();
if ([Link]() == [Link])
{
[Link] = [Link];
boja = [Link];
//[Link] =boja;
color = [Link]().ToString("x");
color = [Link](2, 6);
color = "#" + color;
[Link] = [Link](color);

SQL NAREDBE
string query = "Select kategorija_id,kat_ime,kat_opis,pdv,pnp from kategorija";
[Link] = "delete from kategorija where kat_ime=@naziv"
[Link] = "UPDATE kategorija SET kat_opis=@opis,pdv=@pdv WHERE (kat_ime =
@naziv)";
[Link] = "SELECT SUM(iznos) AS Expr2, datum AS Expr1 FROM Fiskal1_STAVKE$
GROUP BY datum";
[Link] = "SELECT datum, naziv, SUM(kolicina) AS Expr2 FROM
Fiskal1_STAVKE$ GROUP BY naziv,datum";
SELECT TOP (1) cijena FROM Fiskal1_STAVKE$ WHERE (naziv = @naziv)";

KLASE
public string ime;
public double osnovica;
public double IznosSaPdv;
public double stopaPdv;
public string curr()
{
string decimale = [Link]("n2");
return decimale;
}
public double izracunajPdv()
{
double rel = stopaPdv / 100;
return osnovica * rel;
}
public double IzbiPdv()
{
double rel = (stopaPdv / 100) + 1;
double iznos = IznosSaPdv / rel;
return IznosSaPdv - iznos;
}
public double IzbiOsnovicu()
{
double rel2 = (stopaPdv / 100) + 1;
return IznosSaPdv / rel2;
}
}

pozivanje klase
Klasa pdv = new Klasa();
[Link] = 2255.33;
[Link] = 25;
[Link] = 12500;

[Link]( "izracunaj pdv:"+[Link]().ToString()+";


IZBI PDV
: "+[Link]().ToString()+";
izbi osnovicu
:"+[Link]().ToString()+", curr: "+ [Link]().ToString());

You might also like