我正在尝试获取“firstName,lastName,assocID等”在我的表单上的数据网格中显示。我是一个新的程序员/脚本小家伙,对不起,如果这是一个愚蠢的问题。我只是不知道如何将associateList.firstName调用到可读的数据网格条目。
我希望datagrid尽可能使用associateList中的每个关联。正在以某种方式考虑索引参考的基本计数器。
关于我如何编写代码的其他输入也很受欢迎。我是新人,也是自学成才。
简而言之:我希望员工可以使用列来显示数据网格中的信息。
数据网格名称是windows窗体上的dataGridAssociates。
namespace Associate_Tracker
{
public partial class Form1 : Form
{
public class Associate
{
//No idea wtf {get; set;} does but I read that I need it?
public string firstName { get; set; }
public string lastName { get; set; }
public string assocRFID { get; set; }
public int assocID { get; set; }
public bool canDoDiverts { get; set; }
public bool canDoMHE { get; set; }
public bool canDoLoading { get; set; }
}
public Form1()
{
InitializeComponent();
}
private void buttonAddAssoc_Click(object sender, EventArgs e)
{
#region Datagrid Creation -- Name: dt
DataTable dt = new DataTable();
dt.Columns.Add("First Name");
dt.Columns.Add("Last Name");
dt.Columns.Add("RFID");
dt.Columns.Add("Associate ID#");
dt.Columns.Add("Diverts");
dt.Columns.Add("MHE");
dt.Columns.Add("Loading");
dataGridAssociates.DataSource = dt;
#endregion
//First & Last name splitter
string allValue = textBoxAssocName.Text;
string firstNameTemp = String.Empty;
string lastNameTemp = String.Empty;
int getIndexOfSpace = allValue.IndexOf(' ');
for (int i = 0; i < allValue.Length; i++)
{
if (i < getIndexOfSpace)
{
firstNameTemp += allValue[i];
}
else if (i > getIndexOfSpace)
{
lastNameTemp += allValue[i];
}
}
firstNameTemp = firstNameTemp.Trim(); // To remove empty spaces
lastNameTemp = lastNameTemp.Trim(); // To Remove Empty spaces
//End splitter
int assocIDTemp; //TryParse succeeds
bool assocIDparse; //Bool for TryParse
//Try Parsing Associate ID to an integer
//Includes catch -> return
assocIDparse = int.TryParse(textBoxAssocID.Text, out assocIDTemp);
if (assocIDparse == false)
{
MessageBox.Show("Please use only numbers in the AssocID input");
return;
}
var associateList = new List<Associate>();
associateList.Add(new Associate
{
firstName = firstNameTemp,
lastName = lastNameTemp,
assocID = assocIDTemp,
canDoDiverts = checkBoxDiverts.Checked,
canDoMHE = checkBoxMHE.Checked,
canDoLoading = checkBoxLoading.Checked,
});
textBoxAssocID.Clear();
textBoxAssocName.Clear();
textBoxRFID.Clear();
}
}
}
答案 0 :(得分:0)
DataTable dt=new DataTable();
dt.clear();
dt.Columns.Add("First Name");
dt.Columns.Add("Last Name");
dt.Columns.Add("RFID");
dt.Columns.Add("Associate ID#");
dt.Columns.Add("Diverts");
dt.Columns.Add("MHE");
dt.Columns.Add("Loading")
DataRow yourDataRow = dt.NewDataRow();
yourDataRow["First Name"] = //assign the value for FirstName here;
yourDataRow["Last Name"] = //assing the value for LastName here;
dt.Rows.Add(yourDataRow);
如果您要在循环中执行此操作..这应该为您提供一个良好的示例,说明从哪里开始..
* 不要忘记在将DataSource分配给DataGrid之后,您将其绑定,如下所示*
GridView1.DataSource=dt;
GridView1.DataBind();
答案 1 :(得分:0)
我不会说这是最优雅的方式,但是使用WinForms和DataGridView控件,您可以使用BindingSource控件来执行此操作。我在下面添加了一个示例,其中包含修改后的代码,以实现您的目标。
这里要注意的要点如下:
任何问题都告诉我。
readonly BindingSource _bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
_bindingSource.DataSource = new List<Associate>();
dataGridAssociates.DataSource = _bindingSource;
}
public class Associate
{
public string firstName { get; set; }
public string lastName { get; set; }
public string assocRFID { get; set; }
public int assocID { get; set; }
public bool canDoDiverts { get; set; }
public bool canDoMHE { get; set; }
public bool canDoLoading { get; set; }
}
private void buttonAddAssoc_Click(object sender, EventArgs e)
{
//First & Last name splitter
string allValue = textBoxAssocName.Text;
string firstNameTemp = String.Empty;
string lastNameTemp = String.Empty;
int getIndexOfSpace = allValue.IndexOf(' ');
for (int i = 0; i < allValue.Length; i++)
{
if (i < getIndexOfSpace)
{
firstNameTemp += allValue[i];
}
else if (i > getIndexOfSpace)
{
lastNameTemp += allValue[i];
}
}
firstNameTemp = firstNameTemp.Trim(); // To remove empty spaces
lastNameTemp = lastNameTemp.Trim(); // To Remove Empty spaces
//End splitter
int assocIDTemp; //TryParse succeeds
bool assocIDparse; //Bool for TryParse
//Try Parsing Associate ID to an integer
//Includes catch -> return
assocIDparse = int.TryParse(textBoxAssocID.Text, out assocIDTemp);
if (assocIDparse == false)
{
MessageBox.Show("Please use only numbers in the AssocID input");
return;
}
var obj = (Associate) _bindingSource.AddNew();
if (obj != null)
{
obj.firstName = firstNameTemp;
obj.lastName = lastNameTemp;
obj.assocID = assocIDTemp;
obj.canDoDiverts = checkBoxDiverts.Checked;
obj.canDoMHE = checkBoxMHE.Checked;
obj.canDoLoading = checkBoxLoading.Checked;
}
textBoxAssocID.Clear();
textBoxAssocName.Clear();
textBoxRFID.Clear();
}