如何在应用程序设置中保存DataGridView [英] How to save a DataGridView in Application Setings

查看:82
本文介绍了如何在应用程序设置中保存DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我一直在尝试这个小时但是没有到达任何地方。



我有一个DataGridView,可以从用户获取值,并在运行时单击保存按钮时更新。

此Datagridview包含组合框和文本框,为了使事情变得更糟,用户提供的输入需要保存为他的首选项的一部分。

说他打开表单,提供输入 A - 单击保存按钮,关闭表单。然后再重新打开表单,输入A应该反映出来。



下面的代码只显示DatagridView中的空白列,没有。行根据用户创建的行增加,但没有填充数据。

我知道,我必须遗漏很多东西但是却无法破解它。

这是我现在所拥有的:



//表单加载

Hi All,

I have been trying this Since hours but not reaching anywhere.

I have a DataGridView which would fetch Values from the User and get updated at Runtime when he clicks the Save Button.
This Datagridview Contains Comboboxes and TextBoxes and to make things worst, the Inputs given by the User need to be saved as part of his Preferences.
Say He opens the Form, Provides an Input A - Clicks Save Button, Closes the Form. Then again Reopens the Form, Input A should be reflected.

The below code just displays Blank Columns in the DatagridView, The no. of Rows increment according to the Rows created by the User, but no Data is populated.
I know, I must be missing a lot of things but just not able to crack it.
Here is what I have till now:

//Form Load

private void Preferences_Load(object sender, EventArgs e)
{

    this.WorkflowGridView1.DataSource = Properties.Settings.Default.WorkflowData;

}





我试图在应用程序设置中保存数据源 - >属性绑定





I was trying to Save the Datasource in the Application Settings->Property Binding

public void Save_Click(object sender, EventArgs e)
       {
           DTable = new DataTable();
           SBind = new BindingSource();

           for (int i = 0; i < WorkflowGridView1.ColumnCount; ++i)
           {
               DTable.Columns.Add(new DataColumn(WorkflowGridView1.Columns[i].Name));
               WorkflowGridView1.Columns[i].DataPropertyName = WorkflowGridView1.Columns[i].Name;

           }

           foreach (DataGridViewRow row in WorkflowGridView1.Rows)
           {
               DataRow rw = DTable.NewRow();
               rw["Technology"] = row.Cells[0].Value;
               rw["Action"] = row.Cells[1].Value;
               rw["Identifier"] = row.Cells[2].Value;
               rw["Value"] = row.Cells[3].Value;
               rw["UserInput"] = row.Cells[4].Value;
               DTable.Rows.Add(rw);
           }

           SBind.DataSource = DTable;
           WorkflowGridView1.DataSource = SBind;


           ////Saving the DataTable and Closing the Form when the Save Button is Clicked
           ////Here WorkfloData is the Variable for Application Settings having Datatable type.

           Properties.Settings.Default.WorkflowData = DTable;
           Properties.Settings.Default.Save();
           this.Close();
       }





P.S.我正在寻找通过应用程序设置的解决方案,任何帮助将不胜感激。



谢谢

Sim



P.S. I am looking out for a Solution through Application Settings only, Any help would be much appreciated.

Thanks
Sim

推荐答案

这篇关于如何在应用程序设置中保存DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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