RT

解决方案 »

  1.   

    datagridView 本身就提供的有这些功能.只需要设置一些熟悉就可以了.
    AllowUserToAddRows,AllowUserToDeleteRows,这些设置为true.
    查询只有自己写代码了.可以做成Excel的数据筛选的效果也可以自己定义查询的编辑器来查询.
    如果是绑定到数据库的那就可以通过设置SQL语句从新查询一次然后再重新绑定到控件上.
      

  2.   

         private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
                SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
                DataSet ds = new DataSet();
                SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
                //sc.Open();
                sda.Fill(ds, "temp");
                //ds.Tables["temp"].Rows[0]["name"] = "sss";
                //int rows=sda.Update(ds, "temp");
                int rowindex = e.RowIndex;
                int columnindex = e.ColumnIndex;
                //label1.Text = rowindex.ToString() + "行"+columnindex.ToString()+"列";
                //string str = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].t);
                if (rowindex > -1 && columnindex > -1)
                {
                    ds.Tables["temp"].Rows[rowindex]["name"] = dataGridView1[1,rowindex].Value.ToString();
                    ds.Tables["temp"].Rows[rowindex]["age"] = dataGridView1[0,rowindex].Value;
                    ds.Tables["temp"].Rows[rowindex]["zip"] = dataGridView1[2,rowindex].Value.ToString();
                    //label1.Text = rowindex.ToString() + "行"+columnindex.ToString()+"列"+dataGridView1[rowindex, columnindex].Value.ToString();
                    //label1.Text  = dataGridView1[0, rowindex].ValueType.ToString();
                }
                sda.Update(ds, "temp");
                //this.shitTableAdapter.Fill(this.masterDataSet1.shit);            //sa.Close();
        }
            //add new item...
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
                DataSet ds = new DataSet();
                SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
                sda.Fill(ds, "temp");
                DataRow dr = ds.Tables["temp"].NewRow();
                dr["name"] = textBox1.Text;
                dr["age"] = Convert.ToInt32(textBox2.Text);
                dr["city"] = textBox3.Text;
                dr["zip"] = textBox4.Text;
                ds.Tables["temp"].Rows.Add(dr);
                sda.Update(ds, "temp");
                this.shitTableAdapter.Fill(this.masterDataSet1.shit);
                shitBindingSource.DataSource = masterDataSet1;
                dataGridView1.DataSource = shitBindingSource;
            }        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
            {
                
            }        private void dataGridView1_Click(object sender, EventArgs e)
            {
                SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
                DataSet ds = new DataSet();
                SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
                sda.Fill(ds, "temp");
                //DataRow dr = ds.Tables["temp"].NewRow();
                string str = dataGridView1.SelectedRows[0].Cells["nameDataGridViewTextBoxColumn"].Value.ToString();
                str1 = "name   =   " + "'" + str + "'";   
                //DataRow[] dr = ds.Tables["temp"].Select(str1);
                //if(dr.Length>0)
                //label1.Text = dr[0]["name"].ToString();
                //dr[0].Delete();
            }        private void button2_Click(object sender, EventArgs e)
            {
                SqlConnection sa = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sa);
                DataSet ds = new DataSet();
                SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
                sda.Fill(ds, "temp");
                DataRow[] dr = ds.Tables["temp"].Select(str1);
                foreach (DataRow datarow in dr)
                {
                    datarow.Delete();
                }
                sda.Update(ds, "temp");
                this.shitTableAdapter.Fill(this.masterDataSet1.shit);
                shitBindingSource.DataSource = masterDataSet1;
                dataGridView1.DataSource = shitBindingSource;
                //dataGridView1.Refresh();
            }
    我自己的,希望对你有用,表中有四列,name(char),age(int),zip(char),city(char)
      

  3.   

    补充一下,
    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)用来更改datagridview中的数据
     private void button1_Click(object sender, EventArgs e)
    是用在外面添加textbox的方法来改数据
      private void dataGridView1_Click(object sender, EventArgs e)和  private void button2_Click(object sender, EventArgs e)它们组合起来可以删除一行,
    datagridview_click把选中行的“name”传给str;str1是select的条件
    button2_Click完成删除