Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writting a program to fill a DataGridView, with a DataGridViewCheckBoxColumn, using Linq, and I have found a problem. Now, my code is like this:
C#
IEnumerable<FileRecord> library;
library = (from file in library_xElement.Descendants("File")
           select new FileRecord
           {
                  Selected = false,
                  Name = file.Element("Name").Value,
                  Size = Convert.ToUInt64(file.Element("Size").Value),
           }).ToList<FileRecord>();
dgvFiles.DataSource = library;
dgvFiles.Refresh();
dgvFiles.Columns["Selected"].ReadOnly = true;

I have declared the DataGridView column &amp;quot;Selected&amp;quot; as a DataGridViewCheckBoxColumn and I have related the DataPropertyName with the Selected property. But, when I run the program, the Selected column is empty, even the checkboxes aren&amp;#39;t present.
Can someone help me? Thanks a lot.

I edit the post to add more sourcecode, so maybe someone could help me.
C#
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
this.Selected = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.FileName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Size = new System.Windows.Forms.DataGridViewTextBoxColumn();
// 
// dgvFiles
// 
this.dgvFiles.AllowDrop = true;
this.dgvFiles.AllowUserToAddRows = false;
this.dgvFiles.AllowUserToDeleteRows = false;
this.dgvFiles.AllowUserToResizeRows = false;
this.dgvFiles.Anchor = ((System.Windows.Forms.AnchorStyles((((System.Windows.Forms.AnchorStyles.Top
            | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
this.dgvFiles.BackgroundColor = System.Drawing.SystemColors.Window;
this.dgvFiles.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dgvFiles.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgvFiles.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dgvFiles.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvFiles.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Selected,
            this.FileName,
            this.Size});
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.5F);
dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dgvFiles.DefaultCellStyle = dataGridViewCellStyle3;
this.dgvFiles.Location = new System.Drawing.Point(0, 49);
this.dgvFiles.Margin = new System.Windows.Forms.Padding(0);
this.dgvFiles.Name = "dgvFiles";
this.dgvFiles.ReadOnly = true;
this.dgvFiles.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.dgvFiles.RowHeadersVisible = false;
this.dgvFiles.RowTemplate.Height = 15;
this.dgvFiles.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dgvFiles.ShowCellErrors = false;
this.dgvFiles.Size = new System.Drawing.Size(776, 426);
this.dgvFiles.TabIndex = 0;
this.dgvFiles.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvFiles_CellContentDoubleClick);
this.dgvFiles.DragDrop += new System.Windows.Forms.DragEventHandler(this.dgvFiles_DragDrop);
this.dgvFiles.DragEnter += new System.Windows.Forms.DragEventHandler(this.dgvFiles_DragEnter);
//
// Selected column definition
//
this.Selected.DataPropertyName = "Selected";
this.Selected.HeaderText = "Seleccionado";
this.Selected.Name = "Selected";
this.Selected.ReadOnly = true;
this.Selected.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Selected.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.Selected.Width = 20;


public class FileRecord
    {
        public bool Selected { get; set; }
        public UInt32 Index { get; set; }
        public string Path { get; set; }
        public string Name { get; set; }
        public UInt64 Size { get; set; }
        public string Shared { get; set; }
        public UInt32 Hits { get; set; }
        public UInt32 Uploads { get; set; }
        public string Rating { get; set; }
        public string Comments { get; set; }
        public string Previewed { get; set; }
        public string Bogus { get; set; }
        public string Sha1 { get; set; }
        public string Md5 { get; set; }
        public string Ed2k { get; set; }
        public string Tiger { get; set; }
        public string Bth { get; set; }
        public string Verified { get; set; }
     }
Posted
Updated 25-Feb-14 7:35am
v4

1 solution

It seems ok, but hard to say with that less code. What I would do is to add a line after linq query.
var qres = files.ToList();

and put a breakpoint at datagrid.datasource set line. when your debugger hits that line qres will show your actual data if any. I think there is something wrong with your data.
I added a sample as the following:

[updated]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
// using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_Grid2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitOthers();
        }


        DataGridViewTextBoxColumn colName;
        DataGridViewTextBoxColumn colSize;
        DataGridViewCheckBoxColumn colSelected;

        private void InitOthers()
        {
            this.colName = new DataGridViewTextBoxColumn()
            {
                Name = "colName",
                HeaderText = "Name",
                DataPropertyName = "Name",
                ValueType = typeof(string)
            };
            this.colSize = new DataGridViewTextBoxColumn()
            {
                Name = "colSize",
                HeaderText = "Size",
                DataPropertyName = "Size",
                ValueType = typeof(string)
            };
            this.colSelected = new DataGridViewCheckBoxColumn()
            {
                Name = "colSelected",
                HeaderText = "Seleccionado",
                DataPropertyName = "Selected",
                Resizable = DataGridViewTriState.True,
                SortMode = DataGridViewColumnSortMode.NotSortable,
                ValueType = typeof(bool)
            };

            this.dataGridView1.Columns.Add(this.colName);
            this.dataGridView1.Columns.Add(this.colSize);
            this.dataGridView1.Columns.Add(this.colSelected);
        }

        private void button_Fill_Click(object sender, EventArgs e)
        {
            IEnumerable<FileRecord> library;
            library = new List<FileRecord>()
            {
                new FileRecord() { Name = "file1", Size = "10", Selected = false },
                new FileRecord() { Name = "file2", Size = "20", Selected = false },
                new FileRecord() { Name = "file3", Size = "30", Selected = false }
            };
            this.dataGridView1.DataSource = library;
            this.dataGridView1.Refresh();
            this.dataGridView1.Columns["colSelected"].ReadOnly = true;
        }
    }

    public class FileRecord
    {
        public string Name { get; set; }
        public string Size { get; set; }
        public bool Selected { get; set; }
    }
}
 
Share this answer
 
v2
Comments
Gabi Cea 21-Feb-14 8:22am    
I have run the code in debug mode, with a breakpoint at datagrid.datasource set line, and the "qres" variable shows me the correct data, namely, all the files in "qres" have the Selected field set to false...
I don't know what happen with this code.
Vedat Ozan Oner 21-Feb-14 9:10am    
you can test it with grid.AutoGenerateColumns=true. add following lines just before setting grid.DataSource:

this.dataGridView1.Columns.Clear();
this.dataGridView1.AutoGenerateColumns = true;
Gabi Cea 21-Feb-14 14:18pm    
I did it, but the result is the same, the Selected column is empty. A curious issue is that, before setting grid.DataSource, the Selected column is correct and I can check it, but, when I set the DataSource, the column becomes empty...
Vedat Ozan Oner 21-Feb-14 16:16pm    
I'm sorry. I think I missed a point. What do you mean by 'before setting grid.DataSource, the Selected column is correct and I can check it'? do you set it somewhere else and you succeed? You provide very little code to tell you about what happens.
Gabi Cea 21-Feb-14 17:03pm    
I mean before setting grid.DataSource, when the grid has got only the data structure, I can check/uncheck the Selected column, but, when I assign data to grid.DataSource, the checkboxes dissapear...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900