.net 7
Form1.cs
using System;
using System.Data;
using System.Windows.Forms;
namespace StudentRecordsApp
{
public partial class Form1 : Form
{
DataTable dt = new DataTable(); // DataTable to store student records
public Form1()
{
InitializeComponent();
InitializeDataTable(); // Setup columns for DataTable
}
private void InitializeDataTable()
{
// Define columns for the table
dt.Columns.Add("Student ID");
dt.Columns.Add("Name");
dt.Columns.Add("Age");
dt.Columns.Add("Course");
// Bind DataTable to DataGridView
dataGridView1.DataSource = dt;
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Validate user input
if (txtStudentID.Text == "" || txtName.Text == "" || txtAge.Text == "" ||
txtCourse.Text == "")
{
MessageBox.Show("Please fill in all fields.", "Missing Information",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Add new record
dt.Rows.Add(txtStudentID.Text, txtName.Text, txtAge.Text, txtCourse.Text);
// Clear text fields after adding
txtStudentID.Clear();
txtName.Clear();
txtAge.Clear();
txtCourse.Clear();
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StudentRecordsApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.Designer.cs
namespace StudentRecordsApp
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtStudentID = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtAge = new System.Windows.Forms.TextBox();
this.txtCourse = new System.Windows.Forms.TextBox();
this.btnAdd = new System.Windows.Forms.Button();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Student ID";
//
// txtStudentID
//
this.txtStudentID.Location = new System.Drawing.Point(74, 0);
this.txtStudentID.Name = "txtStudentID";
this.txtStudentID.Size = new System.Drawing.Size(100, 20);
this.txtStudentID.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(0, 42);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Name";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(0, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(26, 13);
this.label3.TabIndex = 3;
this.label3.Text = "Age";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(0, 136);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 4;
this.label4.Text = "Course";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(74, 42);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(100, 20);
this.txtName.TabIndex = 5;
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(74, 88);
this.txtAge.Name = "txtAge";
this.txtAge.Size = new System.Drawing.Size(100, 20);
this.txtAge.TabIndex = 6;
//
// txtCourse
//
this.txtCourse.Location = new System.Drawing.Point(74, 133);
this.txtCourse.Name = "txtCourse";
this.txtCourse.Size = new System.Drawing.Size(100, 20);
this.txtCourse.TabIndex = 7;
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(41, 182);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 8;
this.btnAdd.Text = "Add Record";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// dataGridView1
//
this.dataGridView1.AutoSizeColumnsMode =
System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(249, -1);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(334, 150);
this.dataGridView1.TabIndex = 9;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(584, 261);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.txtCourse);
this.Controls.Add(this.txtAge);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtStudentID);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtStudentID;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtAge;
private System.Windows.Forms.TextBox txtCourse;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.DataGridView dataGridView1;
}
}
Comments
Post a Comment