You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Syncfusion SfListView示例编译时的CS0103错误?

解决CS0103:名称‘GetDataSource’在当前上下文中不存在的错误

错误原因

  1. GetDataSource是countryInfo.CountryInfo类中的实例方法,你在Form1类中直接调用GetDataSource(),但当前上下文(Form1)并没有定义这个方法,导致编译报错。
  2. CountryInfo类中同时定义了同名的string GetDataSource属性和List<CountryInfo> GetDataSource()方法,存在命名冲突,必须移除其中一个。

修正方案

方案1:实例化对象后调用方法

首先修正CountryInfo类,移除多余的同名属性:

//Class1.cs
using System;
using System.Collections.Generic;

namespace countryInfo
{
    public class CountryInfo
    {
        public string CountryName { get; set; }
        public string Continent { get; set; }

        // 移除该属性,避免命名冲突
        // public string GetDataSource { get; set; }

        public List<CountryInfo> GetDataSource()
        {
            List<CountryInfo> countryInfoCollection = new List<CountryInfo>();
            countryInfoCollection.Add(new CountryInfo() { CountryName = "China", Continent = "Asia" });
            countryInfoCollection.Add(new CountryInfo() { CountryName = "India", Continent = "Asia" });
            return countryInfoCollection;
        }
    }
}

然后在Form1中创建CountryInfo实例,再调用方法:

//form1.cs
using Syncfusion.WinForms.ListView;
using System;
using System.Windows.Forms;
using countryInfo;

namespace Listveiw
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(sfListView1);
            
            // 实例化CountryInfo对象
            CountryInfo dataProvider = new CountryInfo();
            sfListView1.DataSource = dataProvider.GetDataSource();
            
            sfListView1.DisplayMember = "CountryName";
            sfListView1.ValueMember = "Continent";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void sfListView1_Click(object sender, EventArgs e)
        {

        }
    }
}

方案2:将方法改为静态(无需实例化)

修改CountryInfo类的方法为静态:

//Class1.cs
using System;
using System.Collections.Generic;

namespace countryInfo
{
    public class CountryInfo
    {
        public string CountryName { get; set; }
        public string Continent { get; set; }

        // 移除同名属性
        // public string GetDataSource { get; set; }

        // 改为静态方法
        public static List<CountryInfo> GetDataSource()
        {
            List<CountryInfo> countryInfoCollection = new List<CountryInfo>();
            countryInfoCollection.Add(new CountryInfo() { CountryName = "China", Continent = "Asia" });
            countryInfoCollection.Add(new CountryInfo() { CountryName = "India", Continent = "Asia" });
            return countryInfoCollection;
        }
    }
}

在Form1中直接通过类名调用:

sfListView1.DataSource = CountryInfo.GetDataSource();

内容的提问来源于stack exchange,提问作者Md.Istiaq Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:38:19