将datagrid数据保存到数据库 [英] save datagrid data to database

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

问题描述

嗨..
我想将(wpf工具包的)datagrid数据存储到wpf中的数据库中.我正在使用此代码将datagrid转换为datatable..

DataTable dt = new DataTable();
dt =(DataTable)dataGrid1.ItemsSource;

它给了我错误:
无法将类型为"System.Collections.Generic.List"的对象转换为类型为"System.Data.DataTable"."

我如何将datagrid转换为datatable ??

还有其他方法可以将datagrid的数据保存到数据库吗?

谢谢.

Hi..
i want to store datagrid(of wpf toolkit) data into database in wpf. i am using this code to cast datagrid to datatable..

DataTable dt = new DataTable();
dt = (DataTable)dataGrid1.ItemsSource;

it gives me error:
"Unable to cast object of type ''System.Collections.Generic.List to type ''System.Data.DataTable''."

how can i cast datagrid to datatable???

is there any other method to save data of datagrid to database?

thanks.

推荐答案

你好,
我试图解决此查询的问题是在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();
           
        }
    }
}



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

谢谢&问候
基数:)



Do rate my answer once you find it useful

Thanks & Regards
Radix :)


浏览此msdn
Go through this msdn post - it will give you a few ideas on how to achieve this.


请阅读本文,以及有关WPF Datagrid及其它的很多知识.您的操作应该很清楚:
WPF DataGrid实用示例 [
Have a look at this article and a lot about WPF Datagrid and it''s operations should be clear to you:
WPF DataGrid Practical Examples[^]


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

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