我尝试使用Telerik UI为WinForms创建一个分层GridView,其中包含两个用于获取数据的SQL查询。
但他们使用Northwind DataSet。 (我不会通过VS2008 SQL连接向导连接到我的SQL数据库。)
private void LoadUnboundData()
{
using (radGridView1.DeferRefresh())
{
for (int i = 0; i < nwindDataSet.Employees.Count; i++)
...
如何通过SQLDataReader使用我自己的数据集?
我正在使用C#的Visual Studio 2008和带有AdventureWorks数据库示例的SQL Express 2012 Server。
欢迎提出建议 最诚挚的问候
答案 0 :(得分:0)
以下是一篇描述如何使用数据阅读器的文章:http://www.telerik.com/help/winforms/gridview-populating-with-data-binding-to-datareader.html
答案 1 :(得分:0)
如果您要执行此“ OnDemand”或立即预加载所有内容,我并不满意。您可以先参考我在Telerik论坛上提供的有关OnDemand负载的答案,然后对其进行更改以适合您的需求。该逻辑将使用一个数据表,您可以从任何数据源(如ODBC)加载该数据表。
链接:https://www.telerik.com/forums/ondemand-hierarchical-datagrid#Cy41z5kzRUG-44fhLidpeQ
代码示例:
using System;
using System.Data;
using Telerik.WinControls.UI;
namespace example
{
public partial class frmtest : Telerik.WinControls.UI.RadForm
{
private DataTable _parentData = new DataTable();
public frmtest()
{
InitializeComponent();
}
private void frmtest_Load(object sender, EventArgs e)
{
_parentData = exampleParentDataTableLoad(); // Replace with call to populate a datatable from a query
loadGridAndParentRecords();
}
private void loadGridAndParentRecords()
{
exampleRadGridView.DataSource = _parentData;
var _childTemplate = CreateChildTemplate();
exampleRadGridView.Templates.Add(_childTemplate);
_childTemplate.HierarchyDataProvider = new GridViewEventDataProvider(_childTemplate);
exampleRadGridView.RowSourceNeeded += ExampleRadGridView_RowSourceNeeded;
}
private GridViewTemplate CreateChildTemplate()
{
GridViewTemplate template = new GridViewTemplate();
template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
template.Columns.Add(new GridViewTextBoxColumn
{
Name = "Id",
HeaderText = "ID"
});
template.Columns.Add(new GridViewTextBoxColumn
{
Name = "SampleNumber",
HeaderText = "Sample Number"
});
template.Columns.Add(new GridViewTextBoxColumn
{
Name = "ParentId",
HeaderText = "Parent Id"
});
return template;
}
private void ExampleRadGridView_RowSourceNeeded(object sender, Telerik.WinControls.UI.GridViewRowSourceNeededEventArgs e)
{
if (e.ParentRow != null)
{
if (e.ParentRow.DataBoundItem is DataRowView rowViewData)
{
var _childDataTable = exampleChildTableLoad(); // Replace with call to populate a datatable from a query
var _dataRows = _childDataTable.Select($"ParentId = {rowViewData["Id"]}");
foreach (DataRow row in _dataRows)
{
GridViewRowInfo _newRowObj = e.Template.Rows.NewRow();
_newRowObj.Cells["Id"].Value = row["Id"];
_newRowObj.Cells["SampleNumber"].Value = row["SampleNumber"];
_newRowObj.Cells["ParentId"].Value = row["ParentId"];
e.SourceCollection.Add(_newRowObj);
}
}
}
}
private DataTable exampleParentDataTableLoad()
{
DataTable _return = new DataTable();
_return.Columns.Add("Id", typeof(int));
_return.Columns.Add("DisplayName", typeof(string));
_return.Rows.Add(1, "Test1");
_return.Rows.Add(2, "Test2");
return _return;
}
private DataTable exampleChildTableLoad()
{
DataTable _return = new DataTable();
_return.Columns.Add("Id", typeof(int));
_return.Columns.Add("SampleNumber", typeof(string));
_return.Columns.Add("ParentId", typeof(int));
_return.Rows.Add(9999, "Test1", 2);
_return.Rows.Add(9998, "Test2", 2);
_return.Rows.Add(9997, "Test3", 2);
_return.Rows.Add(9996, "Test4", 1);
_return.Rows.Add(9995, "Test5", 1);
_return.Rows.Add(9994, "Test6", 1);
return _return;
}
}
}