如何在datagridview中做 [英] how to do in datagridview

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

问题描述

我们只能将数据加载到datagrid视图中还是可以将记录从gridview插入数据库中?如果我们可以将记录从gridview插入多个记录到数据库中,该怎么办?请帮助代码.........关于bishnu

hi frens, can we only load datas into datagrid view or we can also insert records into database from gridview? if we can insert records multiple records into database from gridview, how to do so? please help with the code.........with regards bishnu

推荐答案

您可以使用DataGridView添加,更新,删除数据.

检查以下代码,它将帮助您解决问题:
You can add,update,delete data using DataGridView.

Check the below code it will help you to solve your problem :
public partial class DataTrialForm : Form
    {
        private String connectionString = null;
        private SqlConnection sqlConnection = null;
        private SqlDataAdapter sqlDataAdapter = null;
        private SqlCommandBuilder sqlCommandBuilder = null;
        private DataTable dataTable = null;
        private BindingSource bindingSource = null;
        private String selectQueryString = null;
        public DataTrialForm()
        {
            InitializeComponent();
        }
//In the Form Load event set data source for the DataGridView control.
private void DataTraiForm_Load(object sender, EventArgs e)
        {
            connectionString = ConfigurationManager.AppSettings["connectionString"];
            sqlConnection = new SqlConnection(connectionString);
            selectQueryString = "SELECT * FROM t_Bill";
            sqlConnection.Open();
            sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
            sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
            dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable);
            bindingSource = new BindingSource();
            bindingSource.DataSource = dataTable;
            dataGridViewTrial.DataSource = bindingSource;
            // if you want to hide Identity column
            dataGridViewTrial.Columns[0].Visible = false;
        }
//To update, insert or delete data in database from DataGridView have a look at this code snippet.
private void addUpadateButton_Click(object sender, EventArgs e)
        {
            try
            {
                sqlDataAdapter.Update(dataTable);
            }
            catch (Exception exceptionObj)
            {
                MessageBox.Show(exceptionObj.Message.ToString());
            }
        }
  }


尝试一下
使用datgridview更新
try this
Updation with datgridview


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

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