0% found this document useful (0 votes)
6 views5 pages

Allocateclassbasics

The document provides an updated code snippet for modifying a DataGridView in a Windows Forms application to include additional columns for class allocation details. It includes code for connecting to a database, retrieving student details, and adding columns for class allocation date, allocated class, image upload, and activation buttons. The document also outlines event handling for button clicks to upload images and activate records in the database.

Uploaded by

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

Allocateclassbasics

The document provides an updated code snippet for modifying a DataGridView in a Windows Forms application to include additional columns for class allocation details. It includes code for connecting to a database, retrieving student details, and adding columns for class allocation date, allocated class, image upload, and activation buttons. The document also outlines event handling for button clicks to upload images and activate records in the database.

Uploaded by

Othniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

In that case, you can modify the existing DataGridView to include the columns for stdclassallocated.

Here's an updated code snippet that assumes the DataGridView already exists:

```

private void Form1_Load(object sender, EventArgs e)

// Create a connection string

string connectionString = "Your Connection String";

// Create a query

string query = @"

SELECT

studentId,

StudentSurname,

StudentName

FROM

stdstudentdetails

";

// Create a DataTable

DataTable dataTable = new DataTable();

// Fill the DataTable

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();

using (SqlCommand command = new SqlCommand(query, connection))


{

using (SqlDataReader reader = command.ExecuteReader())

dataTable.Load(reader);

// Add columns for stdclassallocated

DataGridViewTextBoxColumn columnClassAllocationDate = new DataGridViewTextBoxColumn();

columnClassAllocationDate.HeaderText = "Class Allocation Date";

columnClassAllocationDate.Name = "ClassAllocationDate";

dataGridView1.Columns.Add(columnClassAllocationDate);

DataGridViewComboBoxColumn columnClassAllocated = new DataGridViewComboBoxColumn();

columnClassAllocated.HeaderText = "Class Allocated";

columnClassAllocated.Name = "ClassAllocated";

string[] studentClasses = { "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6" };

columnClassAllocated.Items.AddRange(studentClasses);

dataGridView1.Columns.Add(columnClassAllocated);

DataGridViewTextBoxColumn columnImage = new DataGridViewTextBoxColumn();

columnImage.HeaderText = "Image";

columnImage.Name = "Image";

dataGridView1.Columns.Add(columnImage);

DataGridViewButtonColumn columnButton = new DataGridViewButtonColumn();


columnButton.HeaderText = "Upload Image";

columnButton.Name = "UploadImage";

columnButton.Text = "Upload Image";

columnButton.UseColumnTextForButtonValue = true;

dataGridView1.Columns.Add(columnButton);

DataGridViewButtonColumn columnActivate = new DataGridViewButtonColumn();

columnActivate.HeaderText = "Activate";

columnActivate.Name = "Activate";

columnActivate.Text = "Activate";

columnActivate.UseColumnTextForButtonValue = true;

dataGridView1.Columns.Add(columnActivate);

// Add event handlers for button clicks

dataGridView1.CellContentClick += DataGridView1_CellContentClick;

// Bind the DataTable to the DataGridView

dataGridView1.DataSource = dataTable;

private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

if (e.ColumnIndex == dataGridView1.Columns["UploadImage"].Index)

OpenFileDialog openFileDialog = new OpenFileDialog();

if (openFileDialog.ShowDialog() == DialogResult.OK)

{
string filePath = openFileDialog.FileName;

// Save the file path to the corresponding cell in column Image

dataGridView1.Rows[e.RowIndex].Cells["Image"].Value = filePath;

else if (e.ColumnIndex == dataGridView1.Columns["Activate"].Index)

// Get the values from the DataGridView

string studentId = dataGridView1.Rows[e.RowIndex].Cells["studentId"].Value.ToString();

string classAllocationDate =
dataGridView1.Rows[e.RowIndex].Cells["ClassAllocationDate"].Value.ToString();

string classAllocated =
dataGridView1.Rows[e.RowIndex].Cells["ClassAllocated"].Value.ToString();

string image = dataGridView1.Rows[e.RowIndex].Cells["Image"].Value.ToString();

// Create a connection string

string connectionString = "Your Connection String";

// Create a query

string query = @"

INSERT INTO stdclassallocated (studentId, classAllocationDate, classAllocated, image)

VALUES (@studentId, @classAllocationDate, @classAllocated, @image)

";

// Create a SqlCommand

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();
using (SqlCommand command = new SqlCommand(query, connection))

command.Parameters.AddWithValue("@studentId", studentId);

command.Parameters.AddWithValue("@classAllocationDate", classAllocationDate);

command.Parameters.AddWithValue("@classAllocated", classAllocated);

command.Parameters.AddWithValue("@image", image);

command.ExecuteNonQuery();

MessageBox.Show("Record activated successfully!");

```

Replace "Your Connection String" with your actual connection string.

You might also like