我该如何解决这个问题? [英] How do I solve this problem?

查看:81
本文介绍了我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想在另一个网格VIEW(VISUAL STUDIO 2015)中复制数据网格视图,然后将数据保存到数据库中。



我是什么尝试过:



Hello, I want to copy data grid view in another grid VIEW( VISUAL STUDIO 2015) and after data to save in database.

What I have tried:

private void tsButtonCopy_Click(object sender, EventArgs e)
        {
            // loop to check if the checkbox cell is checked
            for (int i = 0; i <= questionDataGridView.Rows.Count - 1; i++)
            {
                // rowAlreadyExist => if the row already exist on dataGridView2
                bool rowAlreadyExist = false;
               bool checkedCell = (bool)questionDataGridView.Rows[i].Cells[7].Value; (HERE I HAVE A PROBLEM  : An unhandled exception of type 'System.InvalidCastException' occurred in PROJECT.exe

Additional information: Specified cast is not valid.
                if (checkedCell == true)
                {
                    DataGridViewRow row = questionDataGridView.Rows[i];

                    // the dataGridView2 have one row or more
                    if (questionDetailDataGridView.Rows.Count != 0)
                    {
                        // loop to see if the row already exist on dataGridView2
                        for (int j = 0; j <= questionDetailDataGridView.Rows.Count - 1; j++)
                        {
                            if (row.Cells[0].Value.ToString() == questionDetailDataGridView.Rows[j].Cells[0].Value.ToString())
                            {
                                rowAlreadyExist = true;
                                break;
                            }
                        }

                        // add if the row ont exist on dataGridView2
                        if (rowAlreadyExist == false)
                        {
                            questionDetailDataGridView.Rows.Add(row.Cells[0].Value.ToString(),
row.Cells[1].Value.ToString(),
row.Cells[2].Value.ToString(),
row.Cells[3].Value.ToString(),
row.Cells[4].Value.ToString(),
row.Cells[5].Value.ToString(),
row.Cells[6].Value.ToString(),
row.Cells[7].Value.ToString() );
                        }
                    }

                    // add if the dataGridView2 have no row 
                    else
                    {
                        questionDetailDataGridView.Rows.Add(row.Cells[0].Value.ToString(),
row.Cells[1].Value.ToString(),
row.Cells[2].Value.ToString(),
row.Cells[4].Value.ToString(),
row.Cells[5].Value.ToString(),
row.Cells[6].Value.ToString(),
row.Cells[7].Value.ToString() );

推荐答案

Quote:

我在我的代码中写了这个问题

bool checkedCell =(bool)questionDataGridView.Rows [i] .Cells [7] .Value; (这里我有一个问题:PROJECT.exe中发生'System.InvalidCastException'类型的未处理异常

I wrote the problem in my code
bool checkedCell = (bool)questionDataGridView.Rows[i].Cells[7].Value; (HERE I HAVE A PROBLEM : An unhandled exception of type 'System.InvalidCastException' occurred in PROJECT.exe



原因很简单:DGV中的任何内容都不是 bool ,是不是可以投出一个。



这需要您的数据和您的应用程序按顺序运行找出它是什么,为什么它在那里 - 它可能是你的数据源中的坏价值,它可能是错误的列,它可能是一个空行:我们不知道,我们可以找不到因为我们无法访问您的数据或正在运行的应用程序。



所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。一个快速的谷歌Visual Studio调试器应该给你所需的信息。



在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看代码,并查看数据并找出shou ld手动发生。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


The reason for that is simpel: whatever is in your DGV isn't a bool, is can;t be cast to one.

That needs your data and your app running in order to find out what it is, and why it;s there - it could be a "BAD VALUE" in your data source, it could be the wrong column, it could be an empty row: we don;t know, and we can't find out because we have no access to your data or your running app.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. A quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆