Interfeys oynasini yaratish
B iz Programmaning interfeysini yaratip olishimiz kerak. Unung uchun bizga DataGridView, Button elementlaridan foydalanamiz.
Formamizni ismini spisok deb nomlaymiz.
Endi Formamizga панель элементов bo`limidan DataGridView elementini tanlab Formamizga qoshamiz.
DataGridView elementimizning orqa fonini oq rang qilib qoyamiz. Buning uchun DataGridView elementining Свойства xosasasining BackgroundColor qismidan oq rangni tanlaymiz.
D ataGridViewga ustunlarni qoshib chiqishimiz kerak buning uchun Добавить столбец xosasasini tanlaymiz va bizga uning oynasi ochiladi unnan текст заголовка qismiga ustunning nomini kiritamiz va Добавить tugmasi bosiladi.
Birinchi ustinimizni nomi Qurilish obiktlari uchin ajratilgan materiallar
Ikkinchi ustinimizni nomi Materiallar soni
Uchinchi ustinimizni nomi Narxi
To’rtinchi ustinimizni nomi ishlatilgan materiallar umumiy narxi
Besinchi ustinimizni nomi Qurilish kompaniyasi obiektlari soni
Oltinchi ustinimizni nomi Ajratiladigan umumiy mablag'
Yettinchi ustinimizni nomi Ishlatiladigan materiallar umumiy narxi
Shu bilan barcha ustunlarimizni kiritib chiqamiz.
Foydalanuvchi malumotlarini kiritgan vaqtida malumotlari qalin qilib korinishi uchun DataGridView xosasalar qismiga kirib DefaultCellStyle qismiga kiramiz
va bizga shu bolimning oynasi ochiladi unnan Font qismini tanlab shrift formatini qoyamiz va OK tugamsi bosiladi.
Endi DataGridView ustuda ishlanadigan amallarni xosil qilamiz.
Nusxalash
Accesska saqlash
Excelga
Nusqalash bu tugma bosilgan vaqitda DataGridViewdagi malumot nusxalanishi kerak. Buning uchun Nusqalash tugmasining Click xosasasidan foydalanamiz. Tugma ichidagi kodni quydagicha yozamiz.
private void copyAlltoClipboard() { dataGridView1.SelectAll(); DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null) Clipboard.SetDataObject(dataObj); }
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.SelectAll();
}
N atijani quydagicha ko`rinishta boladi:
Accesska saqlash bu tugma bosilgan vaqitda DataGridViewdagi malumotlar Accesska saqlanishi kerak. Buning uchun Accesska saqlash tugmasining Click xosasasidan foydalanamiz. Tugma ichidagi kodni quydagicha yozamiz.
private void button3_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count != 1)
{
MessageBox.Show("Bir yacheykani tanlang!!!");
return;
}
int index = dataGridView1.SelectedRows[0].Index;
if (dataGridView1.Rows[index].Cells[0] == null || dataGridView1.Rows[index].Cells[1] == null ||
dataGridView1.Rows[index].Cells[2] == null || dataGridView1.Rows[index].Cells[3] == null)
{
MessageBox.Show("Malumotlar to'liq kiritilmagan!!!");
return;
}
string id = dataGridView1.Rows[index].Cells[0].Value.ToString();
string name = dataGridView1.Rows[index].Cells[1].Value.ToString();
string cost = dataGridView1.Rows[index].Cells[2].Value.ToString();
string quantity = dataGridView1.Rows[index].Cells[3].Value.ToString();
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=Baza.mdb";//bog'lash
OleDbConnection dbConnection = new OleDbConnection(connectionString);
dbConnection.Open();
string query = "INSERT INTO Baza VALUES(" + a1 + ",'" + a2 + "'," + a3 + "," + a4 + "," + a5 + "," + a6 + "," + a7 +")";
OleDbCommand dbCommand = new OleDbCommand(query, dbConnection);//yangi buyruq
if (dbCommand.ExecuteNonQuery() != 1) MessageBox.Show("Xatolik ro'y berdi");
else MessageBox.Show("Baza yangilandi!!!");
dbConnection.Close();
}
Natijani quydagicha ko`rinishta boladi:
Excelga bu tugma bosilgan vaqitda DataGridViewdagi malumotlar Excelga saqlanishi kerak. Buning uchun Excelga tugmasining Click xosasasidan foydalanamiz. Tugma ichidagi kodni quydagicha yozamiz.
private void button4_Click(object sender, EventArgs e)
{
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
Natijani quydagicha ko`rinishta boladi:
Do'stlaringiz bilan baham: |