将datagridviewtextboxcolumn中的数据存储到数据库中 [英] store data from datagridviewtextboxcolumn into database

查看:98
本文介绍了将datagridviewtextboxcolumn中的数据存储到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在开发c#.net中的窗口应用程序,问题是

i已将datagridviewtextboxcolumn添加到datagridview,当选择Student Batch时,相应的学生将填入datagridview现在我有dg中的三列1:学生(所有学生姓名)2:出席/缺席(checbox专栏)3:原因

第3栏是datagridviewtextboxcolumn,现在当我将记录从datagridview保存到数据库时,有两种情况:如果'reason column'单元格包含一些文本,则保存记录b:如果第3列单元格中没有文本,则生成错误对象引用未设置为对象的实例。即如果没有datagridviewtextboxcolumn的文本单元格,那么它的对象就不会被创建

请我快速回复。

解决方案

有不同的方法解决这个问题。



1.你有一个 DataSet ,你用它作为数据的数据源grid

在这种情况下,您可以更改列的行为以返回空字符串而不是DbNull



2.更改使用的代码保存数据

您没有显示您的代码,但这是一个如何操作的示例



假设原因是列的名称。

  string  s =(dg [ 原因,i] ==  null )?  :dg [  Reason,i] .Value.ToString()); 
da.Parameters( @ Reason,s);


Hi all, I am developing window application in c# .net, problem is
i have added datagridviewtextboxcolumn to datagridview, when Student Batch is selected coresponding students are populated in datagridview now i have three columns in dg 1:Student (all students names) 2:Present/Absent (checbox column) 3:Reason
Number 3 column is datagridviewtextboxcolumn, now when i save record from datagridview to database then there are two scenarios a: if 'reason column' cell contain some text then record is saved b:if there is no text in 3rd column cell then error is generated "Object reference not set to an instance of an object." i.e if there is no text cell of datagridviewtextboxcolumn then its object is not created
Please i want quick response.

解决方案

There are different ways to solve this issue.

1. You have a DataSet that you use as the data source for the data grid
In this case you can change the behavior of the column to return an empty string instead of DbNull

2. Change the code used to save the data
You are not showing your code but this an example of how to do it

Assuming Reason is the name of your column.

string s = (dg["Reason", i] == null) ? "" : dg["Reason", i].Value.ToString());
da.Parameters("@Reason", s);


这篇关于将datagridviewtextboxcolumn中的数据存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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