[英]Populate DataGridView
我在Form1
上有一个 DataGridView 。 我需要用其他类/方法中的特定方法填充 Dgv,但我无法访问它,我需要帮助。
DataGridView 的名称是DataConsole
。
这是我需要用来普及 Dgv 的方法:
public static void addrow(string modo, string acao, string info, int tipo_msg, string msg,
string extra, int tipo_extra, string origem, string fase_origem, string metodo)
{
int idrow = DataConsole.Rows.Add();
using (DataGridViewRow row = DataConsole.Rows[idrow])
{
row.Cells["dg_data"].Value = DateTime.Now;
row.Cells["dg_modo"].Value = modo;
row.Cells["dg_acao"].Value = acao;
row.Cells["dg_info"].Value = info;
row.Cells["dg_tipo"].Value = tipo_msg;
row.Cells["dg_msg"].Value = msg;
row.Cells["dg_extra"].Value = extra;
row.Cells["dg_TipoExtra"].Value = tipo_extra;
row.Cells["dg_origem"].Value = origem;
row.Cells["dg_FaseOrigem"].Value = fase_origem;
row.Cells["dg_metodo"].Value = metodo;
}
}
您可以为此使用 DataSource,它比将任何 GrdiRow 添加或更新到网格更容易且更快。 可以在此处找到有关 DataSource 的更多详细信息。 另外,我建议您通过 DataSource 本身获取和设置数据。
选项1
在 class 方法中为 DataGridView 类型的DataConsole
添加一个参数,并将其作为参数传递给调用者。
public static void addrow(
DataGridView dgv,
string modo,
string acao,
string info,
int tipo_msg,
string msg,
string extra,
int tipo_extra,
string origem,
string fase_origem,
string metodo)
{
var idrow = dgv.Rows.Add();
var row = dgv.Rows[idrow];
row.Cells["dg_data"].Value = DateTime.Now;
row.Cells["dg_modo"].Value = modo;
row.Cells["dg_acao"].Value = acao;
row.Cells["dg_info"].Value = info;
row.Cells["dg_tipo"].Value = tipo_msg;
row.Cells["dg_msg"].Value = msg;
row.Cells["dg_extra"].Value = extra;
row.Cells["dg_TipoExtra"].Value = tipo_extra;
row.Cells["dg_origem"].Value = origem;
row.Cells["dg_FaseOrigem"].Value = fase_origem;
row.Cells["dg_metodo"].Value = metodo;
}
然后您可以从frm_Console
调用该方法,如下所示:
public partial class frm_Console : Form
{
//...
private void TheCaller()
{
addrow(DataConsole, //the rest of the parameters....);
}
}
选项 2
将DataConsole
控件的Modifiers
属性设置为Public或者Internal,在class方法中找到它的容器Form,访问:
public static void addrow(
string modo,
string acao,
string info,
int tipo_msg,
string msg,
string extra,
int tipo_extra,
string origem,
string fase_origem,
string metodo)
{
var f = Application.OpenForms.OfType<frm_Console>().FirstOrDefault();
if (f == null) return;
var idrow = f.DataConsole.Rows.Add();
var row = f.DataConsole.Rows[idrow];
row.Cells["dg_data"].Value = DateTime.Now;
row.Cells["dg_modo"].Value = modo;
row.Cells["dg_acao"].Value = acao;
//...etc.
}
选项 3
在DataGridView类型的方法的class中声明一个static变量,在frm_Console
构造函数中设置为DataConsole
。
public class SomeClass
{
public static DataGridView DataConsol;
public static void addrow(
string modo,
string acao,
string info,
int tipo_msg,
string msg,
string extra,
int tipo_extra,
string origem,
string fase_origem,
string metodo)
{
if (DataConsol == null) return; //or throw an exception...
var idrow = DataConsol.Rows.Add();
var row = DataConsol.Rows[idrow];
row.Cells["dg_data"].Value = DateTime.Now;
row.Cells["dg_modo"].Value = modo;
row.Cells["dg_acao"].Value = acao;
//...etc.
}
}
public partial class frm_Console : Form
{
public frm_Console()
{
InitializeComponent();
SomeClass.DataConsol = this.DataConsole;
}
//...
}
选项 4
将 DataConsole-Related 例程保留在控件所属的frm_Console
中。
您遇到了这些问题,因为您没有使用 DataSource 来填充 DataGridView (DGV)。 这不仅会使显示数据变得更加困难,而且还会使获取更改的数据变得更加困难。
DGV 中的每一行都属于同一类型:
class RowData
{
public string Modo {get; set;}
public string Acao {get; set;}
...
}
使用 Visual Studio 设计器创建 DGV 和 BindingSource。 让 BindingSource 显示RowData
的类型。 这将自动为您创建所有列,您可以根据需要手动更改或删除。
您也可以在代码中手动执行此操作:
DataGridView dataConsole = new DataGridView();
DataGridViewColumn columnModo = new DataGridViewColumn(){...}
DataGridViewColumn columnAcao = new DataGridViewColumn(){...}
... // create other columns
dataConsole.Columns.Add(columnModo);
dataConsole.Columns.Add(columnAcao);
... // add other columns
现在重要的部分:
// The data that will be as rows in your table:
BindingList<RowData> dataConsoleContent = new BindingList<RowData>();
或者,如果您已经有一些初始数据:
List<RowData> initialData = ...
BindingList<RowData> dataConsoleContent = new BindingList<RowData>(initialData);
告诉 DGV 它应该显示 BindingList 中的数据:
dataConsole.DataSource = dataConsoleContent;
要添加新行,只需告诉 dataConsoleContent object 创建新行:
RowData newRow = dataConsoleContent.AddNew();
// if you have data: just fill it
newRow.Modo = ...
newRow.Acao = ...
...
并告诉所有显示此 BindingList 的人它已更改:
dataConsoleContent.ResetBinding().
如果用户编辑 DataGridView、添加新行、更改单元格内容或删除行,结果将自动在 BindingList 中:
private void OnButtonFinishedEditing_Clicked(object sender, ...)
{
IEnumerable<DataRow> allDataRows = this.dataConsoleContent;
ProcessEditedData(allDatarows);
}
使用ResetBinding
效率不是很高,因为它会刷新完整的DataGridView。 如果您定期以编程方式更改数据(因此不会由操作员更改),那么您的 DataRow 需要实现 INotifyPropertyChanged。 有关示例,请参见如何:使用 BindingSource 和 INotifyPropertyChanged 接口引发更改通知
这是一些工作。 幸运的是,已经为您完成了几个 nuget 包。 我的最爱之一是 Nuget Equin.ApplicationFramework.BindingListView 。 它为您完成所有 DataSource 工作,此外,它还通过单击列标题进行自动排序。 另一个不同的功能是可以轻松过滤显示的元素,而无需从 DataSource 中删除元素:
List<Customer> customers = GetCustomers();
BindingListView<Customer> view = new BindingListView<Customer>(customers);
dataGridView1.DataSource = view;
在网格中,您现在可以通过单击标题进行排序。 排序顺序是自动完成的,以及调整 header 上的“字形”,它会显示排序方向。 您可以使用 ApplySort 方法以编程方式对视图进行排序。
只显示老客户:
DateTime dateLimit = DateTime.Now - TimeSpan.FromYears(65);
view.ApplyFilter(customer => customer.Birthday < dateLimit});
而且,您只会看到年长的客户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.