简体   繁体   中英

How to Check or Uncheck a Checkbox in Datagridview based on existing data record from Table

I need help on part of this code. I'm using a checkbox column in my DataGridView control. When I retrieve my data record, if a value exists then the checkbox should be checked, and if not it should remain unchecked. How to I accomplish that on a DataGridView with this kind of logic?

using (DataContext dtContext = new DataContext())
{
  var query = (from i in dtContext.materialTBheader where i.proj == Proj_id select i);

  foreach (var r in query)
  {
    if (!string.IsNullOrEmpty(r.materialheader_id.ToString()))
    {
      string[] row = { r.materialheader_id.ToString(), r.materialname, r.description, string.Format("{0:n2}", r.totalAmount), GetCount(r.materialname, txtMainProjectHeader_id, Convert.ToDecimal(r.totalAmount)), "", -- cell checkbox if record exist true checked if not false uncheck };
      dGVMaterialHeaderList.Rows.Add(row);
    }
  }
}

It dosen't need to add your rows by foreach, use DataSource Property

Suppose you have a List of Person and you want to show in dataGridview, you have to option

1)add your column to data grid in visual studio properties window

2)add your column with coding

then map your data to grid here is an simple example to help yo

    public class Person
    {
        public int Id { get; set; }
        public string LastName { get; set; }
        public bool Married { get; set; }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
       //your data from ef
        var myData = new List<Person>
        {
            new Person{Id=1,LastName="A1",Married =true},
            new Person{Id=1,LastName="A2",Married =false},
            new Person{Id=1,LastName="A3",Married =true},
        };
        //your columns
        var idColumn = new System.Windows.Forms.DataGridViewTextBoxColumn
        {
            Name = "Id",
            HeaderText = "Id",
            DataPropertyName = "Id"
        };

        var lastNameColumn = new System.Windows.Forms.DataGridViewTextBoxColumn
        {
            Name = "LastName",
            HeaderText = "LastName",
            DataPropertyName = "LastName"
        };

        var marriedColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn
        {
            Name="Married",
            HeaderText="Married",
            DataPropertyName= "Married"
        };
        // add your columns to grid
        dGVMaterialHeaderList.Columns.Add(idColumn);
        dGVMaterialHeaderList.Columns.Add(lastNameColumn);
        dGVMaterialHeaderList.Columns.Add(marriedColumn);

        dGVMaterialHeaderList.AutoGenerateColumns = false;
        //bind your data
        dGVMaterialHeaderList.DataSource = myData;
    }

网格数据

In your case the answer is something like below:

using (DataContext dtContext = new DataContext())
        {
            var query = (from i in dtContext.materialTBheader where i.proj == Proj_id select i).ToList();

            var gridData = query.Select(r=>new
            {
                materialheader_id = r.materialheader_id.ToString(),
                r.materialname,
                r.description,
                totalAmount = string.Format("{0:n2}", r.totalAmount),
                Count = GetCount(r.materialname, txtMainProjectHeader_id, Convert.ToDecimal(r.totalAmount)),
                isExist = !string.IsNullOrEmpty(r.materialheader_id.ToString())?true:false
            }).ToList();
            dGVMaterialHeaderList.DataSource = gridData;

        }

I don't know your data structure but I know this is not best approach you choose

I hope this can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM