Взываю к умнейшим! Объясните дураку, что не так делаю? в metroTextBox1 пишу SQL запрос, он мне выводит в dataGridView ответ, но когда я выделяю строку и нажимаю Edit окно не подхватывает значения строки. Я при совершении запроса написал чтобы в BindingSource.DataSource шла this.dataGridView.DataSource, но он все равно говорит мне, что строку я не выделил. Как быть? Помогите пожалуйста. Как заставить эту программу видеть выделенную строку, после того как я ввел запрос в metroTextBox1 ?
Вот мой проект
https://mega.nz/#!NcNglKxK!P5KdaAf2Q...WWHLu3ZKQViDDo
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MetroUI
{
public partial class Form1 : MetroFramework.Forms.MetroForm
{
public Form1()
{
InitializeComponent();
}
int ABZ, AZZZ;
private async void mtAdd_Click(object sender, EventArgs e)
{
using (frmAddEditStudent frm = new frmAddEditStudent(new Student() { Gender = false }))
{
if (frm.ShowDialog() == DialogResult.OK)
{
try
{
studentsBindingSource.Add(frm.StudentInfo);
db.Students.Add(frm.StudentInfo);
await db.SaveChangesAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
this.studentsTableAdapter.Fill(this.dbuiDataSet.Students);
}
private void mtRefresh_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
studentsBindingSource.DataSource = db.Students.ToList();
cityBindingSource.DataSource = db.Cities.ToList();
Cursor.Current = Cursors.Default;
}
DbEntities db;
private void Form1_Load(object sender, EventArgs e)
{
// TODO: данная строка кода позволяет загрузить данные в таблицу "dbuiDataSet.Students". При необходимости она может быть перемещена или удалена.
this.studentsTableAdapter.Fill(this.dbuiDataSet.Students);
}
private async void mtEdit_Click(object sender, EventArgs e)
{
if (ABZ == 1)
{
Student StudentId = studentsBindingSource.Current as Student;
if (StudentId != null)
{
using (frmAddEditStudent frm = new frmAddEditStudent(StudentId))
{
if (frm.ShowDialog() == DialogResult.OK)
{
try
{
studentsBindingSource.EndEdit();
await db.SaveChangesAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
this.studentsTableAdapter.Fill(this.dbuiDataSet.Students);
}
else {
db = new DbEntities();
studentBindingSource.DataSource = db.Students.ToList();
Student StudentId = studentBindingSource.Current as Student;
if (StudentId != null)
{
using (frmAddEditStudent frm = new frmAddEditStudent(StudentId))
{
if (frm.ShowDialog() == DialogResult.OK)
{
try
{
studentsBindingSource.EndEdit();
await db.SaveChangesAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
this.studentsTableAdapter.Fill(this.dbuiDataSet.Students);
}
}
private void mtDelete_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Do you want to delete?", ProductName, MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
foreach (DataGridViewRow Rows in dataGridView.SelectedRows)
{
// это выбранная строка тоже самое что CurrentRow оба возвращают DataGridViewRow
int index = Rows.Index; // индекс выбранной строки
dataGridView.Rows.RemoveAt(index);
this.studentsTableAdapter.Update(this.dbuiDataSet);
}
}
}
private async void mtSave_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("Do you want to save the changes?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
studentsBindingSource.EndEdit();
await db.SaveChangesAsync();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void metroTextBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-UL5AJO7;Initial Catalog=dbui;Integrated Security=True");
con.Open();
SqlDataReader reader = null;
if (this.dataGridView.Visible == true)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Select * from Students WHERE FullName LIKE '" + metroTextBox1.Text + "%'";
var table = new DataTable();
table.Load(cmd.ExecuteReader());
this.dataGridView.DataSource = table;
ABZ = 1;
con.Close();
con.Dispose();
}
if (ABZ==1)
{
studentsBindingSource.DataSource = this.dataGridView.DataSource;
}
}
private void studentsBindingSource_CurrentChanged(object sender, EventArgs e)
{
}
}
}