Deleting from a Data Grid View - winforms

I have created a windows form application. I want this application to be able to use Linq to SQL to search for a record, and then for that record to be selected from a data grid view and deleted.
The form contains a textbox to enter the parameter, a search button and a delete button and a datagrid.
I have the search part working correctly and the data grid is populated but don't know how to implement clicking on the record in the data grid and deleting it.
Update - I have solved the solution. Changes have only been made to the btn_Delete_Click event handler so I have included the updated code for his button after the main code.
namespace DeleteForm
{
public partial class Form1 : Form
{
LinqtoStudentDataContext linqStud = new LinqtoStudentDataContext();
public Form1()
{
InitializeComponent();
}
private void btnDelete_Click(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
var lastName = from stud in linqStud.Students
where txtFind.Text == stud.LastName
select stud;
dataGridView1.DataSource = lastName;
}
}
}
Updated code -
private void btnDelete_Click(object sender, EventArgs e)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
//linqStud.Students.DeleteAllOnSubmit();
linqStud.SubmitChanges();
}
}

First, set selection mode of DataGridView to FullRowSelect. Next, when assigning DataSource you should call ToList() - you can't use query as data source:
private void btnSearch_Click(object sender, EventArgs e)
{
var lastName = txtFind.Text;
var students = from stud in linqStud.Students
where stud.LastName == lastName
select stud;
dataGridView1.DataSource = students.ToList();
}
Get selected rows, and remove databound items (students) from context:
private void btnDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
var student = row.DataBoundItem as Student;
linqStud.Students.Remove(student);
linqStud.SaveChanges();
}
}

Related

Visual Studio 2012 Windows Form application

I am working on Windows Form application on Visual Studio 2012. I have 2 forms.
Add_Item_to_DB1
Add_Item_to_DB2
Both of these forms call a third form called SUBMIT. Now, based on where this form is being called from, it has to submit information to a different database. Everything else in the SUBMIT form is EXACTLY the same except, data is inserted to a different database.
Is there a way to find out where the form is being called from? Kinda new to Form applications.
Thank you
If you open the SUBMIT form with the ShowDialog() method you will be able to determine the form that opened the SUBMIT form via the Owner property. For example:
public partial class Add_Owner_To_Db_1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var submitForm = new SUBMIT();
submitForm.ShowDialog(this);
}
}
public partial class SUBMIT : Form
{
private void SUBMIT_Load(object sender, EventArgs e)
{
//label1.Text will equal "Add_Owner_To_Db_1"
label1.Text = this.Owner.Text;
}
}
Alternatively you can expose a public property on your SUBMIT form that can be populated from the parent form. For example:
public partial class Add_Owner_To_Db_1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
var submitForm = new SUBMIT();
submitForm.ParentName = "Add_Owner_To_Db_1";
submitForm.Show();
}
}
public partial class SUBMIT : Form
{
public string ParentName { get; set; }
private void SUBMIT_Load(object sender, EventArgs e)
{
//label1.Text will equal "Add_Owner_To_Db_1"
label1.Text = ParentName;
}
}
HTH

How to reload datagrid in wpf

I have a WPF application where I want to select item from datagrid and pass to textbox. after that on add button selected gridrow must be remove. I have a stored procedure to remove from table. And at same time reload the table in same datagrid.
I tried this code
private void refresh()
{
datagrid1.items.refresh();
}
private void btnAdd_Click(object Sender, RoutedEventArg e)
{
refresh();
}
private void datagrid1_SelectionChange(object Sender, RoutedEventArg e)
{
var selectedrow = datagrid1.selectedItem as datarowview;
var id = selectedrow["Tagid"]; // Here I get error that object reference is not set is an instance of an object
string s = conver.tostring(id);
txttextbox1.text= s;
}
After click add button, I get the error
Object reference not set to an instance of an object
You are forcing your selected item as datarowView which it is not, cast to correct type
var selectedrow = datagrid1.selectedItem as DataRowView
SelectedItem is type of object that is bound to grid not row
Try doing this
private void datagrid1_SelectionChange(object Sender, RoutedEventArg e)
{
var selectedItem = datagrid1.selectedItem as MY_Custom_Object;
var id = selectedItem.Tagid;
string s = Convert.ToString(id);
txttextbox1.text= s;
}

DataGridView CRUD Operation with LINQ to SQL

I have a datagridview with BindingSource to Linq to SQL as datasource. When I try to insert or delete the data, the gridview is not refreshed.
SampleDataContext context = new SampleDataContext();
BindingSource bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
bindingSource.DataSource = context.Persons;
PersonGridView.DataSource = bindingSource;
}
private void AddButton_Click(object sender, EventArgs e)
{
context.Persons.InsertOnSubmit(new Person { Name = , Address = });
context.SubmitChanges();
}
private void DeleteButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in PersonGridView.SelectedRows)
{
var person = context.Persons.FirstOrDefault(x => x.ID == int.Parse(row.Cells[0].Value.ToString()));
context.Persons.DeleteOnSubmit(person);
}
context.SubmitChanges();
}
Am I missing something here ?
Best Regards,
Brian
Viola after try many solutions I Have a better solution just change insert and delete operation to bindingsource
SampleDataContext context = new SampleDataContext();
BindingSource bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
bindingSource.DataSource = context.Persons;
PersonGridView.DataSource = bindingSource;
}
private void AddButton_Click(object sender, EventArgs e)
{
bindingSource.Add(new Person { Name = "Hello", Address = "Hahahaha123" });
context.SubmitChanges();
}
private void DeleteButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in PersonGridView.SelectedRows)
{
var person = context.Persons.FirstOrDefault(x => x.ID == int.Parse(row.Cells[0].Value.ToString()));
bindingSource.Remove(person);
}
context.SubmitChanges();
}

New tab in WPF - items.add from buttons vs methods

OK, so I have a window called PictureWindow which displays pictures (I've cut out the code not related to making tabs). The TabControl is named "itemsTab". Using a button press, I can make a new tab no problem. But using the same operations inside a called method doesn't work. Using the buttonTab_Click method makes a new tab, the newTab method does not.
The only real difference I can see is due to the sender and RoutedEventArgs objects - how do these effect the operation here? Or is there something else I'm missing?
Thanks in advance.
Edit To make things even stranger, the newTab method does make a new tab, but only if it is called in the PictureWindow constructor method. If I have the following a new tab is made.
public PictureWindow(string current)
{
InitializeComponent();
newTab(current);
}
But if I call the method anywhere else it doesn't work.
public partial class PictureWindow : Window
{
public PictureWindow(string current)
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void buttonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void buttonTab_Click(object sender, RoutedEventArgs e)
{
TabItem newTab = new TabItem();
newTab.Header = "New Tab!";
itemsTab.Items.Add(newTab);
}
public void newTab(string current)
{
TabItem newTab = new TabItem();
itemsTab.Items.Add(newTab);
}
}

How to create a watermark textbox

how to create a watermark text box in winForm
i want to use in my applications login screen
if you want to make it simple, you could do this :
string xyz = "Enter User Name Here..";
private void textBox_Leave(object sender, EventArgs e)
{
{
if (textBox.Text.Length.Equals(0))
{
textBox.Text = xyz;
}
}
}
private void textBox_Click(object sender, EventArgs e)
{
{
if (textBox.Text.Equals(xyz))
{
textBox.Clear();
}
}
}
and do put following lines to your form_load event :
textBox.Text = xyz;
textBox.Select(textBox.Text.Length, 0);

Resources