如何仅更新datagridview中的一个表列 [英] How to update only one table column in datagridview

查看:99
本文介绍了如何仅更新datagridview中的一个表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个datagridview没有连接到数据库只有一列是从表中选择的,并在确认它后端后更新-1,意思是医学名称是XYZ,选择并确认它应该是22-1是21我已经完成了编码,但它显示我索引超出范围。必须是非负且小于集合的大小。参数名称:索引异常 字符串medicinename = dataGridView1.SelectedRows [0] .Cells [Medicine_Name] .Value.ToString();



在datagridview中我没有显示total_available列当用户选择特定药物时应该更新



我尝试了什么:



  string  connectionString =  null ; 
connectionString = ConfigurationManager.ConnectionStrings [ AccessConnectionString]。ConnectionString;
con.ConnectionString = connectionString;
string medicinename = dataGridView1.SelectedRows [ 0 ]。单元格[ Medicine_Name]。Value.ToString();

DialogResult dialogResult = MessageBox.Show( 您确定要插入数据吗? 数据插入确认,MessageBoxButtons.YesNo,MessageBoxIcon.Information);
if (dialogResult == DialogResult.Yes)
{
cmd = new OleDbCommand( update Medicine_Available_Detail set [total_available] = total_available-1其中[Medicine_Name] = @ Medicine_Name,con);
// cmd.Parameters.AddWithValue(@ total_available,medicineavailable);
cmd.Parameters.AddWithValue( @ Medicine_Name,medicinename);
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show( 记录成功更新);
showdata();
dataGridView1.Refresh();



}

解决方案

你需要从调试器开始看看正是你正在处理的事情 - 有可能是SelectedRows属性返回一个空列表,因此在第0行没有元素,所以你得到一个错误的索引异常。

但我们可以'测试它:我们无法访问您的PC或您的数据。

因此在线上放置一个断点,并查看执行时的确切内容。

很可能错误本身不在该代码中,而是应该填充DataGridView或选择一行或多行。我们无法分辨,而且我们没有代码。

因此,使用工具来查看代码运行时到底发生了什么,你应该能够使用它进行。

Hi all ,

I have one datagridview which is not connect to to the database only one column is which is select from table and after confirming it backend it update -1 , means Medicine name is XYZ after selecting and confirming it should be 22-1 is 21 i have done coding for that but its showing me Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index exception on string medicinename = dataGridView1.SelectedRows[0].Cells["Medicine_Name"].Value.ToString();

in datagridview im not showing total_available column its should be update when user selected particular medicine

What I have tried:

string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString; 
            string medicinename = dataGridView1.SelectedRows[0].Cells["Medicine_Name"].Value.ToString();
            
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to insert data", "Data insert Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.Yes)
            {
                cmd = new OleDbCommand("update Medicine_Available_Detail set [total_available]=total_available-1 where [Medicine_Name]=@Medicine_Name", con);
                //cmd.Parameters.AddWithValue("@total_available", medicineavailable);
                cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);
                con.Open();
                int n = cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Record Updated Successfully");
                showdata();
                dataGridView1.Refresh();



            }

解决方案

You need to start with the debugger and see exactly what you are dealing with - the chances are that the SelectedRows property is returning an empty list and thus doesn't have an element at row zero so you get a bad index exception.
But we can't test it: we don't have access to your PC or your data.
So put a breakpoint on the line, and look at exactly what you have when it executes.
It's very likely that the error itself isn't in that code, but in whatever is supposed to be filling the DataGridView or selecting one or more rows. We can't tell, and we don't have the code.
So use the tools you have to look at exactly what is going on while the code is running and you should be able to work it out.


这篇关于如何仅更新datagridview中的一个表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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