Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
hii,everyone

I have a problem when select record by mouse click didn't accept (update,delete) record in datagrid view
but must select record by buttons i make (first record,last record,next,previous) not by mouse click .
this is code update button
C#
int x;

da.UpdateCommand = new SqlCommand("update tblcontacts set firstname=@firstname,lastname=@lastname where id=@id ", cs);
da.UpdateCommand.Parameters.Add("@firstname", SqlDbType.VarChar).Value = textBox1.Text;
da.UpdateCommand.Parameters.Add("@lastname", SqlDbType.VarChar).Value = textBox2.Text;
da.UpdateCommand.Parameters.Add("@id", SqlDbType.BigInt).Value = ds.Tables[0].Rows[tblnamesbs.Position][0];

cs.Open();
x= da.UpdateCommand.ExecuteNonQuery();
cs.Close();

if (x >= 1)
    MessageBox.Show("Record(s) has been updated");

this code display data
C#
da.SelectCommand = new SqlCommand("select * from tblcontacts ", cs);
ds.Clear();
da.Fill(ds);

dt=ds.Tables[0];
dataGridView1.DataSource = ds.Tables[0];
//DataTable dt = new DataTable();

//dt.Columns.Add("firstname", typeof(string));
//dt.Columns.Add("lastname", typeof(string));

tblnamesbs.DataSource = dt;
this.textBox1.DataBindings.Clear();
this.textBox1.DataBindings.Add("Text", this.tblnamesbs, "firstname", true);
this.textBox2.DataBindings.Clear();
this.textBox2.DataBindings.Add("Text", this.tblnamesbs, "lastname", true);
records();

this code go to first record :
C#
tblnamesbs.DataSource = dt;
            tblnamesbs.MoveFirst();
            dgupdate();
            records();
Posted
Updated 18-Jan-14 5:00am
v2
Comments
Christian Graus 18-Jan-14 17:48pm    
You should not use this nonsense. You should write a proper data layer. The stuff you're using is for children to play with computers, not for real programming. Even if you're a child who is learning ( which seems likely to me ), you should still side step this stuff. Really you should learn MVC instead of ASP.NET.
ZurdoDev 18-Jan-14 20:51pm    
I don't understand where you are stuck.
mhassan083 19-Jan-14 1:06am    
select record by mouse click to update it ,not working
ZurdoDev 19-Jan-14 21:16pm    
Why is it not working? Debug it and let us know.

You say 'select record by mouse click to update it ,not working', but, I don't see anywhere that you have written code to make that happen. None of the code you've posted has anything to do with that. You need to handle a click in your HTML, in order to trigger all the code you've posted. Where are you doing that ?


Here[^] is an article on what you're trying to do. Please explain, with code, why you think that clicking on a row should allow editing in your case, if you're still stuck.

I just realised this article is WPF, THEN realised you don't actually tell us what you're using. You are not using C# 3.0 AND C# 4.0, but either way, it's not clear if you're using ASP.NET, MVC, winforms, WPF, or what ?
 
Share this answer
 
v2
Comments
Christian Amado 18-Jul-14 11:29am    
+5 for DataGrid Tips & Tricks: Single-Click Editing!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900