C# 如何使用文本框和按钮在datagridview中添加行
因此,我有一个datagridview,其中填充了一个对象列表。 我在表单上添加了3个文本框和一个按钮。问题是如何使用文本框中的文本在datagridview中插入和填充另一行 这是我的班级:C# 如何使用文本框和按钮在datagridview中添加行,c#,asp.net,.net,sharp,C#,Asp.net,.net,Sharp,因此,我有一个datagridview,其中填充了一个对象列表。 我在表单上添加了3个文本框和一个按钮。问题是如何使用文本框中的文本在datagridview中插入和填充另一行 这是我的班级: class Professor { private int id; private string name; private double salary; public Professor() { this.id= 0;
class Professor
{
private int id;
private string name;
private double salary;
public Professor()
{
this.id= 0;
this.name = null;
this.salary= 0;
}
public Professor(int m, string n, double s)
{
this.id= m;
this.name = n;
this.salary= s;
}
}
以下是列表的声明:
ArrayList listProfessors = new ArrayList();
这是填充DataGridView的按钮:
private void addInGridViewFromList_Click(object sender, EventArgs e)
{
string linie;
System.IO.StreamReader file= new System.IO.StreamReader("D:\\Profesor\\Profesor\\Profesori.txt");
while ((line= file.ReadLine()) != null)
{
string[] t = line.Split(',');
listProfessors .Add(new Professor(Convert.ToInt32(t[0]), t[1], Convert.ToDouble(t[2])));
}
file.Close();
dataGridView1.DataSource = listProfessors ;
}
在这个按钮上,我想在DataGridView中手动添加另一行(使用texbox)
private void AddFromKeyboard_Click(object sender, EventArgs e)
{
}
您需要创建一个按钮事件来执行您想要的操作,当按下按钮时,您将添加一个包含您想要的文本框的新行
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text);
}
编辑:因此尝试从按钮向列表中添加新项目:
private void AddFromKeyboard_Click(object sender, EventArgs e)
{
listProfessors.Add(new Professor(Convert.ToInt32(textBox1.Text), textBox2.Text, Convert.ToDouble(textBox3.Text)));
}
在任何事情发生之前,你应该向我们提供一些你正在做一些事情的代码,但是如果没有它,我会尝试向你解释 首先,您可能要做的是下一步:检查您的datagrid的源文件列表,这样您可以从您的文本框中创建一个相应的对象,您可以简单地将其添加到datagrid的源文件列表中,并简单地刷新源文件,如:
dataGrid.ItemsSource=null;
dataGrid.ItemsSource = myCustomList;
或者你可以这样做:
private void btnAdd_Click(object sender, EventArgs e)
{
string firstColum = firstTextBox.Text;
string secondColum = secondTextBox.Text;
string[] row = { firstColum, secondColum };
yourDatagrid.Rows.Add(row);
}
但我再次强调,为了得到完全正确的答案,您应该提供/发布您的代码
不管我希望这能对你有什么帮助都不行。。。但我听你的,我把我的密码贴了出来。也许你可以查一下