I'm having trouble getting the edit button in the datagridview to work properly.I need the edit button when selected to put the text back in the orginal textbox for editting then be able to hit the update button to update that data. I know I'm doing something wrong but I don't know what it is. If someone could look it over and help me figure out what I'm doing wrong that would be great!
This is my code:
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 ProgressReports
{
public partial class ReportsForm : Form
{
public ReportsForm()
{
InitializeComponent();
}
private void ReportsForm_Load(object sender, EventArgs e)
{
string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\eliza\ReportsDatabase.mdf; Integrated Security = True; Connect Timeout = 30";
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Database_tbl", con);
DataSet ds = new DataSet();
da.Fill(ds, "Database_tbl");
ReportDataGrid.DataSource = ds.Tables["Database_tbl"].DefaultView;
this.database_tblTableAdapter.Fill(this.reportsDatabaseDataSet.Database_tbl);
}
private void Submit_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\eliza\ReportsDatabase.mdf; Integrated Security = True; Connect Timeout = 30";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Database_tbl (EntryDate, ReportText) VALUES ('" + DateTime.Now + "','" + ReportText.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Report Successfully Added");
ReportDataGrid.Update();
ReportText.Text = "";
}
//This is where I'm having the problem at
private void ReportDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
{
if (ReportDataGrid.Columns[e.ColumnIndex].Name == "Edit")
{
int i = ReportDataGrid.CurrentCell.RowIndex;
int j = ReportDataGrid.CurrentCell.ColumnIndex + 3;
int x = ReportDataGrid.CurrentCell.ColumnIndex + 1;
ReportText.Text = ReportDataGrid.Rows[i].Cells[j].Value.ToString();
lblRowID.Text = ReportDataGrid.Rows[i].Cells[x].Value.ToString();
btnUpdate.Visible = true;
Submit.Visible = false;
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\eliza\ReportsDatabase.mdf; Integrated Security = True; Connect Timeout = 30";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Update Database_tbl set ReportText = '" + ReportText.Text + "' Where EntryID = " + lblRowID.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Report Successfully Updated");
ReportDataGrid.Update();
ReportText.Text = "";
}
}
}
What I have tried:
This is what I've tried so far
private void ReportDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
{
if (ReportDataGrid.Columns[e.ColumnIndex].Name == "Edit")
{
int i = ReportDataGrid.CurrentCell.RowIndex;
int j = ReportDataGrid.CurrentCell.ColumnIndex + 3;
int x = ReportDataGrid.CurrentCell.ColumnIndex + 1;
ReportText.Text = ReportDataGrid.Rows[i].Cells[j].Value.ToString();
lblRowID.Text = ReportDataGrid.Rows[i].Cells[x].Value.ToString();
btnUpdate.Visible = true;
Submit.Visible = false;
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\eliza\ReportsDatabase.mdf; Integrated Security = True; Connect Timeout = 30";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Update Database_tbl set ReportText = '" + ReportText.Text + "' Where EntryID = " + lblRowID.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Report Successfully Updated");
ReportDataGrid.Update();
ReportText.Text = "";
}
}
}