//dataGridView1  
namespace MySchool
{
    public partial class InStoreroomInsertFrom : Form
    {
        public InStoreroomInsertFrom()
        {
            InitializeComponent();
        }       DataTable dt;
 
       //初始化时 设置好了一个自定义的 DataTable 
        private void InStoreroomInsertFrom_Load(object sender, EventArgs e)
        {
            dt = new DataTable("List");
            dt.Columns.Add("SubjectCode", System.Type.GetType("System.String"));
            dt.Columns.Add("materialcode", System.Type.GetType("System.String"));
            dt.Columns.Add("apellation", System.Type.GetType("System.String"));
            dt.Columns.Add("summary", System.Type.GetType("System.String"));
            dt.Columns.Add("amount", System.Type.GetType("System.String"));
            dt.Columns.Add("MRP", System.Type.GetType("System.String"));
        }
     //单击 button1时 添加数据 到dataGridView1
        private void button1_Click(object sender, EventArgs e)
        {
            DataRow dr = dt.NewRow();
            dr["SubjectCode"] = textBox1.Text;
            dr["materialcode"] = textBox2.Text;
            dr["apellation"] = textBox3.Text;
            dr["amount"] = textBox5.Text;
            dr["MRP"] = textBox4.Text;
            dr["summary"] = textBox6.Text;
            dt.Rows.Add(dr);
           dataGridView1.DataSource = dt;
        }                //  单击 button2 时   删除我在  dataGridView1  选中的一行 数据          private void button2_Click(object sender, EventArgs e)
        {            //  求助 ???????????????????  这里我要怎么才能删除选中的一行 数据             实现
 
        }
    }
}

解决方案 »

  1.   

    dt.Rows[i].RowState = DataRowState.Deleted;
    然后根据RowState过滤。
      

  2.   


    GridViewRow gvr = GridView1.SelectedRow;
    int id = int.Parse(gvr.Cells[0].Text);
    //SqlConnection conn = new SqlConnection("......");
    //SqlCommand cmd = new SqlCommand("delete from table1 where id="+id+"",conn);
    //conn.Open();
    //cmd.ExecuteNonquery();
      

  3.   

    给你参考:gdvFactory就是DataGridView,dtFactory就是DataTable        private void btnDel_Click(object sender, EventArgs e)
            {
                // 当没有点中单元格时处理异常
                try
                {
                    int identityRow = int.Parse(this.gdvFactory.CurrentRow.Cells["identity"].Value);
                    for (int i = 0; i < dtFactory.Rows.Count; i++)
                    {
                        if (dtFactory.Rows[i].RowState != DataRowState.Deleted)
                        {
                            int indetity = int.Parse(dtFactory.Rows[i]["identity"]);
                            if (identityRow == indetity)
                            {
                                dtFactory.Rows[i].Delete();
                                break;
                            }
                            //调用事件
                           
                        }
                    }                
                }
                catch { }
            }