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

本文介绍如何将Visual Basic中特定的Item索引器语法转换为C#中的等效表达方式,并通过实例展示了两种语言之间的差异。

Item是VB专用的索引器写法,例如:

Rs_dt = dt.Rows(i).Item(Item_Names)

所以不能用于C#,应该这样表达

Rs_dt=dt.Rows[i][Item_Names]

例子还有:

MyItem.Text=DSPageData.Tables["TempID"].Rows[j].Item("SurveyAnswer");

MyItem.Text=DSPageData.Tables["TempID"].Rows[j]["SurveyAnswer"];


System.IndexOutOfRangeException HResult=0x80131508 Message=无法找到列 0。 Source=System.Data StackTrace: at System.Data.DataColumnCollection.get_Item(Int32 index) at System.Data.DataRow.set_Item(Int32 columnIndex, Object value) at 图书管理系统.User.button4_Click(Object sender, EventArgs e) in C:\Users\Administrator\Desktop\项目\图书管理系统\图书管理系统\User.cs:line 79 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at 图书管理系统.Program.Main() in C:\Users\Administrator\Desktop\项目\图书管理系统\图书管理系统\Program.cs:line 19
07-26
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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值