У меня есть приложение базы данных ms-access в C#. Данные таблиц привязаны к DataGridView в C#. Я добавил в мою таблицу новое поле CheckBox с именем Check
.
Когда AllowUserToAddRows
установлено на false
. При загрузке DGV ошибка не генерируется.
Если AllowUserToAddRows
установлено на True
. При загрузке DGV генерируется ошибка. Ошибка находится под: -
введите описание изображения здесь
DGV находится в дочерней форме. Его код приведен ниже.
public partial class frmStudents : Form
{
// Declaration
private DataTable dTable;
public frmStudents()
{
InitializeComponent();
}
private void frmStudents_Load(object sender, EventArgs e)
{
LoadTheme();
// Show Students
StudentsLoadDGV();
}
private void LoadTheme()
{
//foreach (Control btns in this.Controls) Check all controls in Form
foreach(Button btns in this.Controls.OfType<Button>()) // Check only buttons in Form
{
if (btns.GetType() == typeof(Button))
{
Button btn = (Button)btns;
btn.BackColor = ThemeColor.PrimaryColor;
btn.ForeColor = Color.White;
btn.FlatAppearance.BorderColor = ThemeColor.SecondaryColor;
}
}
}
// Getting data from Database to DataTable
private DataTable StudentsDTable()
{
DataTable dt = new DataTable();
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
OleDbCommand command = new OleDbCommand("SELECT s.Check, " +
"DCount("RollNo","students","RollNo <=" & [RollNo]) AS SrNo, s.RollNo, s.SName, "+
"s.FName, s.DOB, c.Class, s.[Section], s.Picture FROM students AS s LEFT JOIN classes "+
"AS c ON s.ClassID = c.ClassID ORDER BY s.RollNo", conn);
conn.Open();
dt.Load(command.ExecuteReader());
}
return dt;
}
// Loading DataTable into DataGridView
private void StudentsLoadDGV()
{
dTable = StudentsDTable();
dgvDisplay.DataSource = dTable;
// Change Column Headings
dgvDisplay.Columns["SrNo"].HeaderText = "Sr No";
dgvDisplay.Columns["RollNo"].HeaderText = "Roll No";
dgvDisplay.Columns["SName"].HeaderText = "Student Name";
dgvDisplay.Columns["FName"].HeaderText = "Father Name";
dgvDisplay.Columns["DOB"].HeaderText = "Date of Birth";
//dgvDisplay.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
private void btnNew_Click(object sender, EventArgs e)
{
DialogResult result;
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
OleDbCommand command = new OleDbCommand("select * from Classes", conn);
conn.Open();
dTable = new DataTable();
dTable.Load(command.ExecuteReader());
frmStudentProfile form = new frmStudentProfile(dTable, "New");
result = form.ShowDialog();
}
if (result == DialogResult.OK)
StudentsLoadDGV();
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (!ValidSelection())
return;
DialogResult result;
// Get the index of the row of selected cell, then get the RollNo from the clicked row and pass it to the command
int index = dgvDisplay.SelectedCells[0].RowIndex;
var selectedRollNo = dgvDisplay.Rows[index].Cells["RollNo"].Value;
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
string query = "select s.RollNo, s.SName, s.FName, s.DOB, c.Class, c.ClassID, s.[Section], s.Picture from Classes as c " +
"left outer join (select * from Students where RollNo = @RollNo) as s on s.ClassID = c.ClassID";
using(OleDbCommand command = new OleDbCommand(query, conn))
{
command.Parameters.Add(new OleDbParameter("RollNo", selectedRollNo));
dTable = new DataTable();
conn.Open();
dTable.Load(command.ExecuteReader());
frmStudentProfile form = new frmStudentProfile(dTable, "default");
result = form.ShowDialog();
}
}
if(result == DialogResult.OK)
StudentsLoadDGV();
}
private void dgvDisplay_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (!ValidSelection())
return;
DialogResult result;
// Get row number that was clicked
var index = e.RowIndex;
// get the RollNo from the clicked row and pass it to the command
var selectedRollNo = dgvDisplay.Rows[index].Cells["RollNo"].Value;
using (var conn = new OleDbConnection(DatabaseObjects.ConnectionString))
{
string query = "select s.RollNo, s.SName, s.FName, s.DOB, c.Class, c.ClassID, s.[Section], s.Picture from Classes as c " +
"left outer join (select * from Students where RollNo = @RollNo) as s on s.ClassID = c.ClassID";
using (OleDbCommand command = new OleDbCommand(query, conn))
{
command.Parameters.Add(new OleDbParameter("RollNo", selectedRollNo));
dTable = new DataTable();
conn.Open();
dTable.Load(command.ExecuteReader());
frmStudentProfile form = new frmStudentProfile(dTable, "default");
result = form.ShowDialog();
}
}
if (result == DialogResult.OK)
StudentsLoadDGV();
}
private bool ValidSelection()
{
bool isValid = true;
string errorMsg = "";
if (dgvDisplay.SelectedRows.Count > 1 )
{
errorMsg += "- Select one Record which you want to Edit.";
isValid = false;
}
else if((dgvDisplay.SelectedCells.Cast<DataGridViewCell>().Select(c => c.RowIndex).Distinct().Count() > 1))
{
errorMsg += "- Select Cells of one Record which you want to Edit.";
isValid = false;
}
if(!isValid)
{
MessageBox.Show(errorMsg, "Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return isValid;
}
}
Как мне удалить указанную выше ошибку ??