GridDataView实现 点击任意一格可以修改

直接上代码好了

private void dgv1Member_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    string name = this.dgv1Member.Columns[this.dgv1Member.CurrentCell.ColumnIndex].HeaderText;
    this.dgv1Member.BeginEdit(true); //将单元格设为编辑状态
    if(name == "会员号")
    {
        this.dgv1Member.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true; //将当前单元格设为可读
        MessageBox.Show("会员名唯一,不允许修改");
    }
    else
    {
        this.dgv1Member.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = false; //将当前单元格设为可读
    }
    string MemberID = this.dgv1Member.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); //获取当前单元格MemberID
}
private void dgv1Member_CellEndEdit(object sender, DataGridViewCellEventArgs e) { string aa = dgv1Member.Columns[this.dgv1Member.CurrentCell.ColumnIndex].DataPropertyName; //Name string aa1 = dgv1Member.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); //真真 string aa2 = dgv1Member.Columns[0].DataPropertyName; //Memeerid string aa3 = dgv1Member.Rows[e.RowIndex].Cells[0].Value.ToString(); //id=6 string sql = String.Format("update Member set {0} = '{1}' where {2} ='{3}'", dgv1Member.Columns[this.dgv1Member.CurrentCell.ColumnIndex].DataPropertyName, //所选单元格列名 dgv1Member.Rows[e.RowIndex].Cells[e.ColumnIndex].Value, //所选单元格修改 dgv1Member.Columns[0].DataPropertyName, //所选单元格列名 dgv1Member.Rows[e.RowIndex].Cells[0].Value); //所选行的商品 SqlConnection con = new SqlConnection(); con.ConnectionString = "Data Source=.;Initial CataLog=xiyidian;integrated security=true"; con.Open(); SqlCommand com = new SqlCommand(); com.Connection = con; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); con.Close(); GridViewDataBind(); }

 

posted @ 2018-04-03 16:03  ProZkb  阅读(323)  评论(0编辑  收藏  举报