gpt4 book ai didi

c# - 不包含 "foo"的定义

转载 作者:行者123 更新时间:2023-11-30 20:12:43 26 4
gpt4 key购买 nike

我是 Visual Studio C# 的新手。当我使用 System.Windows.Forms 中的某些方法时,我不断收到以下错误

System.Windows.Forms.DataGridView does not contain a definition for 'foo' and no extension method 'foo' accepting a first argument of type System.Windows.Forms.DataGridView could be found

我的代码如下:

   private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dataGridView1.CurrentRow.Index
textBox2.Text = dataGridView1.Item(2, i).Value
}

正如我所说,我不止一次看到这种错误,但它发生在这里的原因是什么?谢谢

/编辑:根据要求,我添加了整个项目:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

//.Designer
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Dock = DockStyle.None;

PopulateDataGridView();
}
public void PopulateDataGridView()
{
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Nombre";
dataGridView1.Columns[2].Name = "Valor";
dataGridView1.Columns["ID"].HeaderText = "ID";
dataGridView1.Columns["Nombre"].HeaderText = "Nombre";
dataGridView1.Columns["Valor"].HeaderText = "Valor";

//DataGridView.
dataGridView1.Rows.Add(new string[] { "1", "Nachos", "3.00" });
dataGridView1.Rows.Add(new string[] { "2", "Pelukas", "5.70" });
dataGridView1.Rows.Add(new string[] { "3", "Fresas", "3.50" });
dataGridView1.Rows.Add(new string[] { "4", "Mineral", "5.50" });
dataGridView1.Rows.Add(new string[] { "5", "Mirinda", "7.80" });
dataGridView1.Rows.Add(new string[] { "6", "Jamaica", "2.70" });

// Autosize.
dataGridView1.AutoResizeColumns();
}

private void Form1_Load(object sender, EventArgs e)
{
this.AcceptButton = button7;
textBox1.Focus();
}


[BrowsableAttribute(false)]
public DataGridViewRowCollection Rows { get; }

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dataGridView1.CurrentRow.Index
textBox2.Text = dataGridView1.Item(2, i).Value
}

最佳答案

我怀疑是这一行:

textBox2.Text = dataGridView1.Item(2, i).Value

我可以从 Visual Studio 帮助中看到 Item 是 DataGridView 的属性。此外,它是一个索引器,因此我希望您需要:

textBox2.Text = dataGridView1.Item[2, i].Value

即括号转换为方括号

有了括号,它认为它一定是你所指的函数,但它不是类型内置的,所以它开始寻找扩展方法。

关于c# - 不包含 "foo"的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com