Reputation: 1784
I have written this procedure in SQL and is working
ALTER proc [dbo].[gridcombo] (@tabela int)
as
/*1 Setores */
if (@tabela=1)
begin
select setor_id, setor_nome from tbl_setores
end
/*2 Atividade Economica */
if (@tabela=2)
begin
SELECT cnae_id, cnae_descricao FROM tbl_CNAE
end
I use this procedure to populate a datagridview in C#
public SqlConnection sqlcon = Tconex.GetConnection();
public SqlDataAdapter da2 = new SqlDataAdapter();
public DataTable dt1 = new DataTable();
SqlParameter tabela = new SqlParameter("@tabela", SqlDbType.Int);
tabela.Value = 1;
SqlCommand llena = new SqlCommand("gridcombo", sqlcon);
llena.Parameters.Add(tabela);
llena.CommandType = CommandType.StoredProcedure;
da2.SelectCommand = llena;
da2.Fill(dt1);
BindingSource bsource = new BindingSource();
bsource.DataSource = dt1;
dataGridView1.DataSource = bsource;
dataGridView1.AutoGenerateColumns = true;
sqlcon.Close();
When I change the tabela.value
the datagrid is populated with one or another table, that works fine.
But I cannot edit the table via datagrid
da2.UpdateCommand = new SqlCommandBuilder(da2).GetUpdateCommand();
da2.Update(dt1);
sqlcon.Close();
What am I doing wrong? Why can't I update the table??
Thanks for any help!
Upvotes: 2
Views: 803
Reputation: 29000
You must return your PrimaryKey
of table in your SelectCommand
, in order to generate correctly your update command
Link : http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlcommandbuilder%28v=vs.80%29.aspx
Upvotes: 2