简体   繁体   English

CRUD在C#中使用Datagridview

[英]CRUD Using Datagridview in C#

I have got an error information when I tried to execute my coding program in C#. 当我尝试在C#中执行编码程序时出现错误信息。 I try to Insert the data using datagridview in C# but when I tried, I have got this error information. 我尝试在C#中使用datagridview插入数据,但是当我尝试时,我得到了此错误信息。 "An unhandled exception of type 'Oracle.DataAccess.Client.OracleException' occurred in Oracle.DataAccess.dll Additional information: External component has thrown an exception" . “在Oracle.DataAccess.dll中发生了类型为'Oracle.DataAccess.Client.OracleException'的未处理异常。附加信息:外部组件引发了异常” Here is my coding program; 这是我的编码程序;

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        INST_NO = dataGridView1.Rows[e.RowIndex].Cells["INST_NO"].Value.ToString();

        if (INST_NO == "")
        {
            INST_NO1 = 0;
        }
        else
        {
              INST_NO1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["INST_NO"].Value.ToString());
        }
        if (INST_NO1 == 0)
        {
            OracleCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO TMCI_IM_PROD VALUES('" + dataGridView1.Rows[e.RowIndex].Cells["INST_NO"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["ITM_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["LINE_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_LOC_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_SCHD_QTY"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_ST_SCHD_DT"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_END_SCHD_DT"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["ORD_STS_TYP"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["CAVITY"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["RESULT_1"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["RESULT_2"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["TRANSFER_1"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["TRANSFER_2"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["INS_TS"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["INS_USR_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["UPD_TS"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["UNIT_WT"].Value.ToString() + "')";
            cmd.ExecuteNonQuery();
            fill_grid();
        }
        else
        {
            OracleCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "UPDATE TMCI_IM_PROD SET INST_NO='" + dataGridView1.Rows[e.RowIndex].Cells["ITM_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["LINE_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_LOC_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_SCHD_QTY"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_ST_SCHD_DT"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["PROD_END_SCHD_DT"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["ORD_STS_TYP"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["CAVITY"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["RESULT_1"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["RESULT_2"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["TRANSFER_1"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["TRANSFER_2"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["INS_TS"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["INS_USR_CD"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["UPD_TS"].Value.ToString() + "', '" + dataGridView1.Rows[e.RowIndex].Cells["UNIT_WT"].Value.ToString() + "' WHERE INST_NO=" + INST_NO1 + "";
            cmd.ExecuteNonQuery();
            fill_grid();
        }

    }

I have tried with many other ways, but I did not find it, how to overcome this problem. 我已经尝试了许多其他方法,但没有找到如何克服此问题的方法。 Does anyone here could help me to solve this problem? 这里有人可以帮助我解决这个问题吗? Thank you in advance. 先感谢您。

This isn't the way a datagridview is supposed to be used, but to explain it properly would quite a large undertaking. 这不是应该使用datagridview的方式,但是正确地解释它将是一项巨大的工作。 Basically, datagridview shouldn't be used as a storage of data, it should just be connected to a datasource (a DataTable)and that is the data storage container. 基本上,datagridview不应用作数据存储,而应仅连接到数据源(DataTable),即数据存储容器。 You just use the DGV as a device for the user to click on, type in etc to change the data in the datatable, and then you use a tableadapter to send the updates back to the database. 您只需将DGV用作用户单击的设备,键入etc即可更改数据表中的数据,然后使用tableadapter将更新发送回数据库。 Go here: https://msdn.microsoft.com/en-us/library/fxsa23t6.aspx and start by doing the "Creating a Simple Data Application" tutorial 转到此处: https : //msdn.microsoft.com/zh-cn/library/fxsa23t6.aspx并通过执行“创建简单数据应用程序”教程开始

This way your app ends up a better representation of MVC theory - model is the datatable, view (a device that shows data) and control (a device that changes data) is done by the datagridview 这样,您的应用程序可以更好地表示MVC理论-模型是数据表,视图(显示数据的设备)和控制(更改数据的设备)由datagridview完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM