如何在数据网格视图上正确显示输出? [英] how to properly show the output on a Data Grid View ?
问题描述
我已经编辑了我的最后一个项目,在代码和运行时均未显示错误.
它由两种形式组成:
form1用于添加新对象
Form1.cs
I''ve edited my last project, showing no errors in code nor in run time.
it consists of two forms :
form1 is used to add a new object
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace MobileCompany
{
public partial class Form1 : Form
{
//At the start of the Application, we Create a New Company
static public List<SubcriberLine> Company = new List<SubcriberLine>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//After Entering data into the fields, we Create an Cellphone Line Object
SubcriberLine s = new SubcriberLine(txtName.Text,txtNumber.Text,Convert.ToInt32(txtMobile.Text),
Convert.ToInt32(txtFixed.Text));
//We Add the Cellphone Line Object to the Company's DataBase, Our Arraylist in our company MTN
Company.Add(s);
//confront the MTN Worker that the Adding process has been finished !
MessageBox.Show("new CellPhone Line has been created !");
}
private void button2_Click(object sender, EventArgs e)
{
//In order to show all lines in Our company, we need to SHOW new table, new Page , or a new FORM
Form2 f = new Form2();
f.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
然后按下buttom2,将创建并显示一个名为Form2的新表单,该表单仅包含一个DataGridView,并且在每一行中都将显示每个对象的详细信息,如下所示:
Form2.cs
and by pressing buttom2, a new form , called Form2 is created and shown, this consists of only a DataGridView, and in each line it shows every object with detailed information, just as the following:
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace MobileCompany
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 0; i < Form1.Company.Count; i++)
{
dataGridView1.Rows.Add(Form1.Company[i].Identity,
Form1.Company[i].CustomerName, Form1.Company[i].MobileNumber);
}
}
}
}
可悲的是,用正确的代码思考是完全正确的,我想;仍然,在运行应用程序时,按下buttom2以使用DataGridView打开第二个窗体,它仍然无法显示当前对象-我已将其编码为显示.
任何人都可以分享和帮助,这就是编码的全部内容.
sadly, it''s completely right thinking with the proper code-I guess; still, when running the application,pressing buttom2 to open the 2nd form with the DataGridView, it still can''t show the current objects- which I''ve coded it to show.
Anyone can share and help, that''s what coding is all about !
推荐答案
对于52位看过我的问题却无法解决像我这样的菜鸟或新手自己解决的问题.
现在我想到的错误是,没有列就没有行!
因此,考虑一下,我需要一个带有列标题名称的固定列,并且应该在加载表单时将其写成,这里是Form2加载:
Form2.cs
It''s really shameful for the 52 persons who viewed my question was unable to solve a problem, that a rookie or a newbie like me has solved it on his own.
now the mistake I''ve thought about is, there''s no rows without columns!
so, come to think of it, I needed a fixed columns with a columns'' header name, and it should be written when the form loads, here Form2 loads :
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace MobileCompany
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//the columms.Add Method takes only two strings, the 1st is //a referral name, and the second is for the output name of the column
dataGridView1.Columns.Add("id","ID");
dataGridView1.Columns.Add("customername","Customer Name");
dataGridView1.Columns.Add("mobile","Mobile Number");
//now we clear the Rows each time, so there would be no duplicates
dataGridView1.Rows.Clear();
//and here we start adding and stop till we reach the end of list.
for (int i = 0; i < Form1.Company.Count; i++)
{
dataGridView1.Rows.Add(Form1.Company[i].Identity,
Form1.Company[i].CustomerName, Form1.Company[i].MobileNumber);
}
}
}
}
这篇关于如何在数据网格视图上正确显示输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!