Click here to Skip to main content
15,900,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have added checkbox coulumn in DataGridview but I can't select any checkbox.
What am I missing?
Thank you in advance.

C#
DataSet ds;
        DataTable dt;
        SQLiteConnection conn;
        SQLiteCommand comm;
        SQLiteDataAdapter sda;
        private int PageSize = 5;
        private int CurrentPageIndex = 1;
        private int TotalPage = 0;
        string strConn = @"Data Source=C:\carku\carku.db";
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            First_Grid();
        }
        private void First_Grid()
        {
            conn = new SQLiteConnection(strConn);

            conn.Open();
            comm = new SQLiteCommand("select price,Thumnail_url  From cars order by price", conn);


            sda = new SQLiteDataAdapter(comm);

            ds = new DataSet();
            sda.Fill(ds, "cars");


            CalculateTotalPages();
            dataGridView1.ReadOnly = true;

           
            DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
            CheckBox chk = new CheckBox();
            CheckboxColumn.Width = 20;
            CheckboxColumn.Name = "col_checkbox";
            
            dataGridView1.Columns.Add(CheckboxColumn);
          

            dataGridView1.Columns.Add("price", "price");
            var dvi = new DataGridViewImageColumn { HeaderText = "thumnail_url" };
            dataGridView1.Columns.Add(dvi);
           

            // int row = ds.Tables["cars"].Rows.Count - 1;
            int row = GetCurrentRecords(CurrentPageIndex, conn).Rows.Count - 1;
            for (int r = 0; r <= row; r++)
            {
                dataGridView1.Rows.Add();
                //  dataGridView1.Rows[r].Cells[0].Value = ds.Tables["cars"].Rows[r].ItemArray[0];
                dataGridView1.Rows[r].Cells[1].Value = GetCurrentRecords(CurrentPageIndex, conn).Rows[r].ItemArray[0];

                //to show image from url on column cells[3]
                System.Net.WebClient webSource = new System.Net.WebClient();
                //string url = ds.Tables["cars"].Rows[r].ItemArray[1].ToString();
                string url = GetCurrentRecords(CurrentPageIndex, conn).Rows[r].ItemArray[1].ToString();
                byte[] data = webSource.DownloadData(url);
                System.IO.MemoryStream pipe = new System.IO.MemoryStream(data);
                Image jpgImage = Image.FromStream(pipe);
                dataGridView1.Rows[r].Cells[2].Value = jpgImage;

            }
            lbl_Page.Text = "1/" + TotalPage.ToString();

        }


What I have tried:

I have commented like this //CheckBox chk = new CheckBox(); but didn't work.
Posted
Updated 11-May-16 23:32pm

1 solution

Remove the line
C#
dataGridView1.ReadOnly = true;


[EDIT] - Came back to this. It occurred to me that you don't want the user overtyping any of the other columns. So definitely remove the line I mentioned above, but control which of the columns can be adjusted like this...
C#
dataGridView1.Columns[0].ReadOnly = false;
dataGridView1.Columns[1].ReadOnly = true;
 
Share this answer
 
v2
Comments
hapiten 12-May-16 5:39am    
Thank you for fixing my problem.
CHill60 12-May-16 5:49am    
My pleasure
Sergey Alexandrovich Kryukov 12-May-16 11:09am    
He-he, 5ed.
—SA
CHill60 12-May-16 11:17am    
Thank you. I smiled too :)

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