从datagrid视图中的文本到可变字符串c#中 [英] Text from datagrid view into variable string c#

查看:104
本文介绍了从datagrid视图中的文本到可变字符串c#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我有一个数据网格视图,我想从中获取一个项目,然后将其用作选择.我这样做是因为在表格中,我只想保存数据库中已经存在的数据,因为我的业务规则非常严格,所以要避免人们不得不完美地键入数据,他们只需要从数据网格视图中进行选择即可,他们的选择将保存到数据库中.因此,在以前的情况下,我已经实现了这样的功能以获取人员ID,并且效果很好.
我用于个人ID的代码如下:

Hey guys, i have a data grid view that i would like to grab an item from and then use it as a selection. I do this because in the form i only want to save data that is already in the database as my business rules are very strict, so to save people having to type it in perfectly, they just have to select it from the data grid view and their selection will be saved into the database. So in previous cases i have implemented it like this to grab the person ID and it works fine.
the code i used for person ID is as follows:

if (dgvPersonIDMedical.SelectedCells.Count !=0)
            {
                int selectedrowindex1 = dgvPersonIDMedical.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow1 = dgvPersonIDMedical.Rows[selectedrowindex1];
                medPersonID = Convert.ToInt16(selectedRow1.Cells[0].Value);
            }






我现在尝试使用的是一个字符串,所以我尝试了一个convert.to字符串,但它不起作用,这是我的带有插入语句的代码






the one i am trying to use now is a string so i tried a convert.to string and it is not working, here is my code with the insert statement

if (dgvMedical.SelectedCells.Count !=0)
            {
                int selectedrowindex1 = dgvMedical.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow1 = dgvMedical.Rows[selectedrowindex1];
                conditionType = Convert.ToString(selectedRow1.Cells[0].Value);
            }


            try
            {
                medicalTableAdapter.Insert(Convert.ToInt32(medPersonID), conditionType, medicareTXT.Text, doctorTXT.Text, healthFundTXT.Text, memberNumberTXT.Text, ambulancefundTXT.Text, medicationTXT.Text);
            }

            catch (Exception)
            {
                MessageBox.Show("Error. That ID already exists within the medical table");

            }





我的代码可以很好地编译,但是不断弹出catch块,这意味着我在某个地方弄乱了东西,有人可以告诉我在哪里吗?





my code comppiles fine but keeps bringing up the catch block which means i messed something up somewhere, can anyone tell me where?

thanks for your time!

推荐答案

否.对不起.

但是-您可以告诉自己它在哪里.

如果可能导致开发失败,请在MessageBox行上放置一个断点,并检查异常本身. SQL Server非常擅长报告问题,它可以帮助您解决造成问题的原因.您也可以返回并再次执行插入,这一次单步进入方法直到出现问题.还有一个大锤子的方法:VS菜单,调试".选择例外",并确保每个复选框都有一个"Trown"和"User unhandled"标记.按确定.现在,无论何时抛出异常,您都将使VS停止,因此您可以确切地了解引起该异常的原因.这可能是PITA,具体取决于您的应用程序的编写方式...

如果仅在生产中发生,则在消息框前添加代码以将所有内容(我的意思是所有内容)记录到文本文件中,以便稍后阅读(无论如何记录错误都是一个好主意,这使调试更加容易).以这种方式发现问题并不是那么简单,但是它可能会给您足够的机会在开发条件下复制问题.
No. Sorry.

But - you can tell yourself where it is.

If you can cause a failure in development, then put a breakpoint on the MessageBox line, and examine the exception itself. SQL server is pretty good at reporting problems, and it can help you quite a bit with what it causing it. You can also go back and execute the Insert again, this time single-stepping into the method until you get the problem. There is also the big-hammer approach: VS Menu, "Debug". Select "Exceptions" and make sure every single checkbox has a tick, both "Thrown" and "User unhandled". Press OK. You will now get VS stopping whenever an exception is thrown, so you can see exactly what is causing it. This may be a PITA depending on how defensively your app is written...

If it only occurs in production, then add code before the message box to log everything (and I mean everything) to a text file so you can read it later (it is a good idea to log errors anyway, it makes debugging easier). It''s not as simple to find the problem that way, but it may give you enough to replicate the problem in development conditions.


这篇关于从datagrid视图中的文本到可变字符串c#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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