“System.Data.DataRow”并不包含“Item”的定义

本文探讨了C#与VB.NET中访问IEnumerable接口对象的不同方式。C#采用索引器如title.Text=ds.Tables[guestbook].Rows[0][title];而VB.NET则使用Item属性进行类似操作,例如myDs.Tables[emp].Rows[e.Item.ItemIndex][last_name]=Name.Text。

用的是C#语言,而C#访问实现IEnumerable接口的对象是使用索引器,就是这样写:
title.Text=ds.Tables["guestbook"].Rows[0]["title"];
而Item属性是VB.Net专用的索引器写法。

myDs.Tables["emp"].Rows[e.Item.ItemIndex]【last_name"】= Name.Text;

 

using ExcelDataReader; using Mirror; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using UnityEngine; using UnityEngine.UI; public class MgrInit : MonoBehaviour { [SerializeField] public Button startBtn; [SerializeField] public GameObject Panels; [SerializeField] public Button pathSettingBtn; [SerializeField] public GameObject pathSettingPanel; public GameObject setIsHostPanel; public Button resetBtn; public Image Bg; public List<ItemData> itemList;// 存储读取到的Excel数据 public List<IntroSkillData> guestMediaTable;// 存储读取到的Excel数据 public List<WeekTableItem> weekTable;// 存储读取到的Excel数据 void Start() { //这个代码必须有不然打包后读取不到文件 System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // 1. 指定外部 Excel 路径(需提前放置到可访问位置) LoadExcelAll(); EventSystem.Instance.Register<EventRefreshExcel>((EventRefreshExcel) => { LoadExcelAll(); UIMgr.Instance.guestMediaTable = guestMediaTable; UIMgr.Instance.weekTable = weekTable; EventGetExcel eventGetExcel = new EventGetExcel(); eventGetExcel.itemList = itemList; UIMgr.Instance.itemDatas=itemList; EventSystem.Instance.Trigger(eventGetExcel); UIMgr.Instance.UpdateWeekRank(); UIMgr.Instance.UpdateMediaTable(); }); } private void LoadExcelAll() { string filePath = ImagePath.BATTLE_EXCEL_ICON_PATH; itemList=ParseExcel(LoadExcel(filePath)); filePath = ImagePath.GUESTNAME_MEDIA_TABLE; guestMediaTable=ParseSkillExcel(LoadExcel(filePath)); filePath = ImagePath.WEEK_TABLE_SCORE; weekTable = ParseWeekExcel(LoadExcel(filePath)); for(int i=0;i< itemList.Count; i++) { itemList[i].Account= guestMediaTable.Find(x => x.guestName==itemList[i].Name).guestMediaName; } } public DataTable LoadExcel(string filePath) { if (string.IsNullOrEmpty(filePath)) return null; try { using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // 3. 创建读取器(自动识别 xls/xlsx) using (var reader = ExcelReaderFactory.CreateReader(stream)) { // 4. 获取整个 Excel 内容(转为 DataSet) DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration() { // 重要配置:将第一行设为列名 ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true } }); // 5. 遍历所有工作表 return result.Tables[0]; } } }catch(System.Exception e) { Debug.LogError(e.Message); return null; } // 2. 读取文件 } List<ItemData> ParseExcel(DataTable table) { if(table == null)return null; List<ItemData> itemList = new List<ItemData>(); // 跳过第一行(列标题) for (int row = 0; row < table.Rows.Count; row++) { DataRow currentRow = table.Rows[row]; ItemData item = new ItemData() { // 按列索引获取(确保列顺序匹配) ID = System.Convert.ToInt32(currentRow[0]), Name = currentRow[1].ToString(), Account=currentRow[2].ToString(), }; itemList.Add(item); } return itemList; } List<IntroSkillData> ParseSkillExcel(DataTable table) { if (table == null) return null; List<IntroSkillData> itemList = new List<IntroSkillData>(); // 跳过第一行(列标题) for (int row = 0; row < table.Rows.Count; row++) { DataRow currentRow = table.Rows[row]; IntroSkillData item = new IntroSkillData() { // 按列索引获取(确保列顺序匹配) id = System.Convert.ToInt32(currentRow[0]), guestName = System.Convert.ToString(currentRow[1]), guestMediaName = System.Convert.ToString(currentRow[2]), }; itemList.Add(item); } return itemList; } List<WeekTableItem> ParseWeekExcel(DataTable table) { if (table == null) return null; List<WeekTableItem> itemList = new List<WeekTableItem>(); // 跳过第一行(列标题) for (int row = 0; row < table.Rows.Count; row++) { DataRow currentRow = table.Rows[row]; WeekTableItem item = new WeekTableItem() { // 按列索引获取(确保列顺序匹配) guestName = System.Convert.ToString(currentRow[0]), totalScore = System.Convert.ToInt32(currentRow[1]), weekScore = System.Convert.ToInt32(currentRow[2]), dayScore = System.Convert.ToInt32(currentRow[3]), }; itemList.Add(item); } return itemList; } } [System.Serializable] public class ItemData { public int ID; public string Name; public string Account; } [System.Serializable] public struct IntroSkillData { public int id;//顺序id public string guestName;//嘉宾的名字 public string guestMediaName;//嘉宾的媒体名称 } [System.Serializable] public class WeekTableItem { public string guestName;//嘉宾的名字 public int totalScore;//总分 public int weekScore;//本周分数 public int dayScore;//当天分数 } public static class WeekTableItemSerialization { public static void WriteWeekTableItem(this NetworkWriter writer, WeekTableItem item) { writer.WriteInt(item.totalScore); writer.WriteInt(item.weekScore); writer.WriteInt(item.dayScore); writer.WriteString(item.guestName); } public static WeekTableItem ReadWeekTableItem(this NetworkReader reader) { return new WeekTableItem() { totalScore = reader.ReadInt(), weekScore = reader.ReadInt(), dayScore = reader.ReadInt(), guestName = reader.ReadString() }; } } public static class IntroSkillDataSerialization { public static void WriteWeekTableItem(this NetworkWriter writer, IntroSkillData item) { writer.WriteInt(item.id); writer.WriteString(item.guestName); writer.WriteString(item.guestMediaName); } public static IntroSkillData ReadWeekTableItem(this NetworkReader reader) { return new IntroSkillData() { id = reader.ReadInt(), guestName = reader.ReadString(), guestMediaName = reader.ReadString() }; } }在代码中把这个Excel表修改写入本地怎么做
09-04
using CsvHelper; using CsvHelper.Configuration; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CsvHelper第三方 { public partial class Form1 : Form { public DataTable dataTable = new DataTable();//表格数据类型 可以添加行数据 public string currentPath;//文件路径 public Form1() { InitializeComponent(); //dataTable.Columns.Add("ID",typeof(int));//添加一列 类型是整型的 //DataRow row1 = dataTable.NewRow();//创建一个新行 //row1["ID"] = 1;//给新行的每一列添加值 //DataSource 表格空间的数据源 //绑定数据源 dataGridView1.DataSource = dataTable; //是否为每一个新列创建对应的数据,如果要是等于true,数据会自动绑定到新列上 //dataGridView1.AutoGenerateColumns = true; //dataGridView1.AutoGenerateColumns = false; //不会自动创建新列 必须手动创建所以的列并且设置数据绑定, } //读取 private void button1_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog())//打开一个文件对话框 { ofd.Filter = "CSV文件(*.csv)|*.csv";//过滤文件类型 只保留csv文件 ofd.Title = "打开csv文件"; if (ofd.ShowDialog()==DialogResult.OK) { //ofd.ShowDialog()打开对话框 //DialogResult.OK 对话框的结果是ok //读取路径下的csv //文件路径 ofd.FileName //MessageBox.Show(ofd.FileName); LoadCsvFile(ofd.FileName);//自己封装的一个函数 根据路径读取内容 } } } //加载csv public void LoadCsvFile(string filePath) { //尝试执行代码 try { //CsvConfiguration 创建csv读取或写入的一些配置 //using System.Globalization; //CultureInfo.InvariantCulture 文化区域设置,尽量保证不同国家的格式不一样的话,也能正常解析 //1.创建csv读取或写入的一些配置 var configation = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ",", //分割符号设置 HasHeaderRecord = true, //是否有表头的设置 MissingFieldFound = null, //如果缺失字段的处理 BadDataFound = null, //错误数据的处理 Encoding = Encoding.UTF8, //编码的设置 }; //2.创建c#StreamReader读取工具 using (var reader = new StreamReader(filePath, Encoding.Default)) //创建读取工具 //3.创建csvhelper的读取对象 参数需要StreamReader对象 using (var csv = new CsvReader(reader,configation)) { //4.读取数据到datatable //CsvDataReader 数据读取对象 using (var r1 = new CsvDataReader(csv)) { dataTable.Clear(); //先清空dataTable dataTable.Load(r1); //添加本次的数据 } } currentPath = filePath; //保存当前文件的路径 MessageBox.Show("文件加载成功"); } catch (Exception ex) //如果出错了 捕获异常 { //DivideByZeroException 除数为0的异常 //IndexOutOfRangeException 索引超出界限的异常 //ArgumentException 参数为null的异常 //Exception 所有异常类的基类 //数组越界的异常 //throw;//抛出异常 MessageBox.Show("文件加载失败"); } } //写入 private void button2_Click(object sender, EventArgs e) { var configation = new CsvConfiguration(CultureInfo.InvariantCulture) { Delimiter = ",", //分割符号设置 HasHeaderRecord = true, //是否有表头的设置 MissingFieldFound = null, //如果缺失字段的处理 BadDataFound = null, //错误数据的处理 Encoding = Encoding.Default, //编码的设置 }; using (var sw1 = new StreamWriter(currentPath,false, Encoding.Default)) using (var csv = new CsvWriter(sw1, configation)) { //写入表头 foreach (DataColumn item in dataTable.Columns) //遍历所有列 { csv.WriteField(item.ColumnName); //把所有的列的标题写入csv文件中 } csv.NextRecord(); //开始下一个记录 //写入表数据 foreach (DataRow item in dataTable.Rows)//遍历所有的行 { for (int i = 0;i < dataTable.Columns.Count;i++)//遍历每一列 { //item[i]?.ToString()??"" 如果item[i]处转成字符串,如果不存在设置为" " csv.WriteField(item[i]?.ToString()??""); } //一行结束后 在进行第二行的写入 csv.NextRecord(); } } MessageBox.Show("写入成功"); } //添加csv数据 int count = 0; Random r = new Random(); private void button3_Click(object sender, EventArgs e) { count++; DataRow row1 = dataTable.NewRow(); // 创建新行 row1["姓名"] = "卤蛋" + count + "号"; row1["年龄"] = r.Next(18,50); row1["性别"] = "男"; row1["工资"] = r.Next(1000, 5000); dataTable.Rows.Add(row1); } } } csv文件里有姓名这一列,添加数据为什么显示没有姓名这一列列
最新发布
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值