Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It shows following error:--

System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Windows.Forms.DataGridViewImageColumn'.'

What I have tried:

namespace TRIMURTI_POJECT
{
    public partial class Form2 : Form
    {
       
        SqlConnection con;
        SqlDataAdapter adpt;
        DataTable dt;

        public object dgvDisplayTiles { get; private set; }
        public object ConditionTypes { get; private set; }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Admin\source\repos\DATABASE\data2.sql.mdf;Integrated Security=True;Connect Timeout=30");
            con.Open();
            adpt = new SqlDataAdapter("Select * from Details ", con);
            dt = new DataTable();
            adpt.Fill(dt);
            dataGridView.DataSource = dt;
            
        
            DataGridViewImageColumn imgcol = new DataGridViewImageColumn();
            imgcol=(DataGridViewImageColumn)dataGridView.Columns[14];
            imgcol.ImageLayout = DataGridViewImageCellLayout.Stretch;


            adpt.Dispose();









            con.Close();

           
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

       

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Admin\source\repos\DATABASE\data2.sql.mdf;Integrated Security=True;Connect Timeout=30");
            con.Open();
            adpt = new SqlDataAdapter(" select * from Details Where [Part No] like '%"+txtSearch.Text+"%'",con);
            dt = new DataTable("Details");
            adpt.Fill(dt);
            dataGridView.DataSource = dt;
            con.Close();
            txtSearch.Width = 300;
            txtSearch.Height = 70;
            txtSearch.Font = new Font("Bold", 15,FontStyle.Bold);
            
           dataGridView.Rows[1].DefaultCellStyle.BackColor = Color.Red;
            

        }
Posted
Updated 1-Jan-18 23:30pm
v2
Comments
Karthik_Mahalingam 2-Jan-18 5:26am    
in c# most of the collections works on zero based index, make sure 15th column in the grid is an image column

1 solution

That's the full error? It doesn't give you a filename and line number?
I'm gussing it's this line:-

imgcol=(DataGridViewImageColumn)dataGridView.Columns[14];

and that column [14] is a text column and that you intended to use a different column.

Why on earth are you addressing columns by ordinal number anyway? Its prone to causing erorrs just like this one...
 
Share this answer
 
Comments
Member 13602958 2-Jan-18 6:09am    
i dont get it...please give me coding to solve this error.

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