Member 13602958 Ответов: 1

Пожалуйста, дайте мне решение:-


Он показывает следующую ошибку:--

Система.InvalidCastException: "невозможно привести объект типа" система.Окна.Формы.DataGridViewTextBoxColumn' to type 'System.Окна.Формы.DataGridViewImageColumn'.'

Что я уже пробовал:

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;
            

        }

Karthik_Mahalingam

в c# большинство коллекций работает с нулевым индексом, убедитесь, что 15-й столбец в сетке является столбцом изображения

1 Ответов

Рейтинг:
0

pt1401

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...


Member 13602958

я не понимаю...пожалуйста, дайте мне кодирование, чтобы решить эту ошибку.