将默认值设置为update datagridview中的特定字段

时间:2013-04-09 11:58:27

标签: c# winforms datagridview

我使用datagridview在Windows表单中显示SELECT查询我使用此代码

    SqlCommand sCommand;
        SqlDataAdapter sAdapter;
        SqlCommandBuilder sBuilder;
        DataSet sDs;
        DataTable sTable;         
private void form1_Load(object sender, EventArgs e)
            {   
                string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirecto  ry|\\Database1.mdf;Integrated Security=True;User Instance=True";
                string sql = "SELECT * FROM mytable where col2 =1";
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
                sCommand = new SqlCommand(sql, connection);
                sAdapter = new SqlDataAdapter(sCommand);
                sBuilder = new SqlCommandBuilder(sAdapter);
                sDs = new DataSet();
                sAdapter.Fill(sDs, "mytable");
                sTable = sDs.Tables["mytable"];
                connection.Close();
                dataGridView1.DataSource = sDs.Tables["mytable"];
                dataGridView1.ReadOnly = true;
                save_btn.Enabled = false;
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }

            private void new_btn_Click(object sender, EventArgs e)
            {
                dataGridView1.ReadOnly = false;
                save_btn.Enabled = true;
                new_btn.Enabled = false;
                delete_btn.Enabled = false;
            }

            private void delete_btn_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("Are you sure?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    dataGridView1.Rows.RemoveAt(dataGridView1.Selected  Rows[0].Index);
                    sAdapter.Update(sTable);
                }
            }

            private void save_btn_Click(object sender, EventArgs e)
            {
                sAdapter.Update(sTable);
                dataGridView1.ReadOnly = true;
                save_btn.Enabled = false;
                new_btn.Enabled = true;
                delete_btn.Enabled = true;
            }
        }

但我想在col2的插入/编辑值为1

0 个答案:

没有答案