URGENT...how to create delete button coding in simple windows form application ?

Posted by Hemachandranvperumal under C# on 11/6/2012 | Points: 10 | Views : 1714 | Status : [Member] | Replies : 2
below my coding is there..
my coding,, i can not delete the row ...
delete button got the error.
i need the delete button coding pls urgent...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace databinding
{
public partial class Form1 : Form
{
DataTable dt;
DataRow dr;
string code;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'hema1DataSet.hemac' table. You can move, or remove it, as needed.
this.hemacTableAdapter.Fill(this.hema1DataSet.hemac);
textName.Enabled = false;
textPlace.Enabled = false;
cmdSave.Enabled = false;

}

private void cmdAdd_Click(object sender, EventArgs e)
{
cmdSave.Enabled = true;
textName.Enabled = true;
textPlace.Enabled = true;
textName.Text = "";
textPlace.Text = "";
dt = hema1DataSet.Tables["hemac"];
cmdAdd.Enabled = false;
}

private void cmdSave_Click(object sender, EventArgs e)
{
dt = hema1DataSet.Tables["hemac"];
dr = dt.NewRow();
dr[0] = textName.Text;
dr[1] = textPlace.Text;
dt.Rows.Add(dr);
hemacTableAdapter.Update(hema1DataSet);
textName.Enabled = false;
textPlace.Enabled = false;
this.hemacTableAdapter.Fill(this.hema1DataSet.hemac);
cmdAdd.Enabled = true;
cmdSave.Enabled = false;
}

private void cmdDelete_Click(object sender, EventArgs e)////THIS BLOCK GOT THE ERROR....CHECK THE BELOW CODING NEED ANSWER PLSS
{
dr = hema1DataSet.Tables["hemac"].Rows.Find(code);
dr.Delete();
hemacTableAdapter.Update(hema1DataSet);
}
}
}




Responses

Posted by: Pooraniidhayabalama on: 11/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try this code to delete a row.

Code :

dr = hema1DataSet.Tables["hemac"].Rows.Find("code");
int index = hema1DataSet.Tables["hemac"].Rows.IndexOf(dr);
hema1DataSet.Tables["hemac"].DefaultView.Delete(index);
hemacTableAdapter.Update(hema1DataSet);

Regards,
Poorani



Hemachandranvperumal, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemachandranvperumal on: 11/6/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks poorani ...
but now i got the error missing primary key exception was handed...
wat i do??


Hemachandranvperumal, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response