从datagrid保存数据时出错 [英] error while saving data from datagrid

查看:63
本文介绍了从datagrid保存数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们
我正在使用窗口应用程序

我正在尝试将数据从数据网格保存到sql服务器,但出现错误

索引超出范围.必须为非负数并且小于集合的大小.参数名称:index


我在更新按钮上写的是

Hi Friends
I am using window application

I am trying to save data from data grid to sql sever but i am getting the error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


The i have written on update button is

try
            {
                string section_id = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string mid = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string section_name = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string sdt = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string indexorder = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
               // string col5 = comboBox1 4,comboBox1.SelectedItem.ToString();

                string insert_sql = "insert into magazinsectionmaster(section_id,mid,section_name,sdt,indexorder)values('" + section_id + "','" + mid + "','" + section_name + "','" + sdt + "','" + indexorder + "')", sc;

                if (this.getcomm(insert_sql))
                {
                    MessageBox.Show("Insert Success");
                }
                else
                {
                    MessageBox.Show("Insert Failed");
                }
            }
            catch
            {
                throw;

            }



当我把断点放到断面和中间时,直接进入断点并给我错误


请帮助我.

在此先感谢



When i put the break point it runs to section and mid then directly it goes to catch and give me error


Please help me in this.

Thanks in advance

推荐答案

我已经自行解决了,但是我一次只能保存一个数据.

I have solve by self but i can able to save one data at a time.

public bool getcomm(string connstring1)
        {
            try
            {
                //SqlConnection sqlcon = this.getsqlcon();
                SqlCommand sqlcomm = new SqlCommand(connstring1, con);
                con.Open();
                sqlcomm.ExecuteNonQuery();
                sqlcomm.Dispose();
                con.Close();
                con.Dispose();
                return true;

            }
            catch (Exception ex)
            {
                return false;
            }







private void btnupdate_Click(object sender, EventArgs e)
        {
            try
            {
              //  string section_id = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();

                //int mid = Convert.ToInt32(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
                //string mid = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string section_name = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                //string sdt = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string indexorder = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();

                // string col5 = comboBox1 4,comboBox1.SelectedItem.ToString();

                string insert_sql = "insert into magazinesectionmaster( section_name, indexorder)values('" + section_name + "','" + indexorder + "')", con;

                if (this.getcomm(insert_sql))
                {
                    MessageBox.Show("Insert Success");
                }
                else
                {
                    MessageBox.Show("Insert Failed");
                }
            }
            catch
            {
                throw;
            }


您可以使用foreach循环读取这样的网格视图行

you can use foreach loop to read grid view rows like this

foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(dataGridView1["your column name", row.Index].Value!=null)
{
  string section_name = dataGridView1["your column name", row.Index].Value.ToString();
}
}



而是使用列索引尝试将列名称用作字符串.

希望对您有帮助.



instead use column index try to use column name as string.

i hope this will help you.


这篇关于从datagrid保存数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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