DataGridView数据编辑 [英] DataGridView data editing

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

问题描述

我有一个绑定到数据源的DataGridView(DGV).现在,我想创建一个过程来在用户完成单元格中的数据编辑后更新此DGV中的数据,但是如果我更改了单元格中的数据并离开单元格,则数据会变回原来的状态.我确定必须设置一些设置,但找不到.
我als0想要向DGV添加一个按钮,然后保存用户编辑的每一行.不幸的是,我不知道如何使该按钮处于活动状态.请帮忙!!!:confused :: confused :: confused::rolleyes:

I have a DataGridView (DGV) that is bound to a datasource. I now want to create a procedure to update the data in this DGV once the user is finished editing the data in the cells but if I change the data in a cell and leave the cell the data changes back. I am sure there must be something setting I must set but can''t find it.
I als0 would like to add a button to the DGV and then save every row that the user edit. Unfortunately I don''t have a clue how to get the event for that button going. Please help!!!:confused::confused::confused: :rolleyes:

推荐答案

hello,
我试图解决此查询的问题是在button1中,我已将网格与数据绑定在一起,在按钮2中,无论您在网格中进行什么编辑,然后单击button2,这些记录都将保存在数据库中
hello,
I have tried to solve this query in button1 i have binded the grid with data and in button 2 i whatever u edit in the grid and hit button2 those records will be saved in the database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace updategrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ds = new DataSet();
        }
        DataSet ds;

        private void button1_Click(object sender, EventArgs e)
        {
            //binding grid to data from database
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select * from details", con); 
            adp.Fill(ds);
            DataTable dt = ds.Tables[0];
            dataGridView1.DataSource = dt;       
            con.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //updating values in grid...
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=phonebook;Integrated Security=True");
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter("select * from details", con); 
            SqlCommandBuilder build = new SqlCommandBuilder(adp);
            adp.Update(ds.Tables[0]);
            con.Close();
           
        }
    }
}


一旦找到有用的答案,就对我的答案进行评分

谢谢&问候
基数:rose:


Do rate my answer once you find it useful

Thanks & Regards
Radix :rose:




我想您正在使用窗口表单和datagridview.

(1)默认情况下,窗口datagridview允许用户通过双击"它来编辑其单元格值.您双击每个单元格即可写该文件.确保属性Readonly未设置为true.

(2)有一个称为"DataSource"的属性.这是设置/获取属性.通过双击单元格进行更新后,数据源将自动更新,并且您可以使用DataSource属性获取更新的(更改的数据). DataSource返回类型为对象"的对象.因此,在进一步使用之前,需要将其强制转换为适当的类型(取决于您是否使用过DataTable,DataView对象,这些对象执行了DataGridView的DataBinding操作.)

Hi,

I guess you are working using window forms and datagridview.

(1)By default window datagridview allows user to edit its cell value by just "double clicking" on it. Each cell becomes writable of you double click on it. Make sure, property Readonly is not set to true.

(2) There is a property called "DataSource". This is set / get property. When you have updated by cell double click, the data source is automatically updated and you get the updated ( chnaged data ) using DataSource property. DataSource returns an object which is of type "object". Therefore, before using further you need to cast it to appropriate type ( depending upon whether you have used DataTable, DataView object which doing DataBinding operation of your DataGridView. )

<br />
<br />
DataTable dT = (DataTable)dataGridView1.DataSource;<br />
<br />



(3)如果在数据表中获得了更新数据,则需要编写相应的代码,并需要更新数据库.

(4)只是错过了有关在您的帖子中添加Button的信息.您可以使用"DataGridViewButtonColumn"类添加按钮列.



(3) If you get the Updated data in data table you need to write appropritae code and need to update the database.

(4) Just missed out about adding Button in your post. You can use "DataGridViewButtonColumn" class to add a button column.

<br />
<br />
DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();<br />
            buttonCol.Name = "ButtonColumnName";<br />
            buttonCol.HeaderText = "Header";<br />
            buttonCol.Text = "Button Text";<br />
dataGridView1.Columns.Add(buttonCol); <br />
<br />


希望对您有帮助.


Hope it helps you,


好,我重新开始显示屏幕,并从头开始执行dgv和datasorces,请不要问我有什么区别"因为我没有知道,但是我可以编辑数据源,并且在dgv之外的Windows窗体中有一个按钮,并且我可以添加,更新和完成所有操作.感谢您的回答,我可能会再来.
OK, I started screen from anew and did the dgv and datasorces from scratch and please, don''t ask me what the difference is ''cause I do not know, but I can edit the datasource and I have a button in my windows form outside of the dgv and I add, update and do everything wonderfully. Thanks for your answers I might come back again.


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

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