本文整理汇总了C#中System.Web.UI.DataSourceView类的典型用法代码示例。如果您正苦于以下问题:C# DataSourceView类的具体用法?C# DataSourceView怎么用?C# DataSourceView使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DataSourceView类属于System.Web.UI命名空间,在下文中一共展示了DataSourceView类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CsvDataSourceView
// The CsvDataSourceView class encapsulates the
// capabilities of the CsvDataSource data source control.
public class CsvDataSourceView : DataSourceView
{
public CsvDataSourceView(IDataSource owner, string name) :base(owner, DefaultViewName) {
}
// The data source view is named. However, the CsvDataSource
// only supports one view, so the name is ignored, and the
// default name used instead.
public static string DefaultViewName = "CommaSeparatedView";
// The location of the .csv file.
private string sourceFile = String.Empty;
internal string SourceFile {
get {
return sourceFile;
}
set {
// Use MapPath when the SourceFile is set, so that files local to the
// current directory can be easily used.
string mappedFileName = HttpContext.Current.Server.MapPath(value);
sourceFile = mappedFileName;
}
}
// Do not add the column names as a data row. Infer columns if the CSV file does
// not include column names.
private bool columns = false;
internal bool IncludesColumnNames {
get {
return columns;
}
set {
columns = value;
}
}
// Get data from the underlying data source.
// Build and return a DataView, regardless of mode.
protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) {
IEnumerable dataList = null;
// Open the .csv file.
if (File.Exists(this.SourceFile)) {
DataTable data = new DataTable();
// Open the file to read from.
using (StreamReader sr = File.OpenText(this.SourceFile)) {
// Parse the line
string s = "";
string[] dataValues;
DataColumn col;
// Do the following to add schema.
dataValues = sr.ReadLine().Split(',');
// For each token in the comma-delimited string, add a column
// to the DataTable schema.
foreach (string token in dataValues) {
col = new DataColumn(token,typeof(string));
data.Columns.Add(col);
}
// Do not add the first row as data if the CSV file includes column names.
if (! IncludesColumnNames)
data.Rows.Add(CopyRowData(dataValues, data.NewRow()));
// Do the following to add data.
while ((s = sr.ReadLine()) != null) {
dataValues = s.Split(',');
data.Rows.Add(CopyRowData(dataValues, data.NewRow()));
}
}
data.AcceptChanges();
DataView dataView = new DataView(data);
if (!string.IsNullOrEmpty(selectArgs.SortExpression)) {
dataView.Sort = selectArgs.SortExpression;
}
dataList = dataView;
}
else {
throw new System.Configuration.ConfigurationErrorsException("File not found, " + this.SourceFile);
}
if (null == dataList) {
throw new InvalidOperationException("No data loaded from data source.");
}
return dataList;
}
private DataRow CopyRowData(string[] source, DataRow target) {
try {
for (int i = 0;i < source.Length;i++) {
target[i] = source[i];
}
}
catch (System.IndexOutOfRangeException) {
// There are more columns in this row than
// the original schema allows. Stop copying
// and return the DataRow.
return target;
}
return target;
}
// The CsvDataSourceView does not currently
// permit deletion. You can modify or extend
// this sample to do so.
public override bool CanDelete {
get {
return false;
}
}
protected override int ExecuteDelete(IDictionary keys, IDictionary values)
{
throw new NotSupportedException();
}
// The CsvDataSourceView does not currently
// permit insertion of a new record. You can
// modify or extend this sample to do so.
public override bool CanInsert {
get {
return false;
}
}
protected override int ExecuteInsert(IDictionary values)
{
throw new NotSupportedException();
}
// The CsvDataSourceView does not currently
// permit update operations. You can modify or
// extend this sample to do so.
public override bool CanUpdate {
get {
return false;
}
}
protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
{
throw new NotSupportedException();
}
}