无法弄清楚如何类字符串转换为一个DataGrid [英] Can't figure out how to convert class strings to a datagrid

查看:166
本文介绍了无法弄清楚如何类字符串转换为一个DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让名字,姓氏,assocID等。在我的表格上一个DataGrid来显示。我是一个新的编程/脚本小子,对不起,如果这是一个愚蠢的问题。我只是不知道如何associateList.firstName打电话可读的数据网格项。



我想如果可能的话用每个员工在associateList DataGrid中。正在考虑对指数refrence莫名其妙基本计数器。



这是我写怎么我的代码其他输入是赞赏为好。我很新的,和自学成才的。



简而言之:我要的同伙使用列的信息分开在DataGrid中显示



数据网格的名称是Windows窗体上dataGridAssociates。

 命名空间Associate_Tracker 
{
公共部分Form1类:表格
{
公共类大专
{
//不知道跆拳道{搞定;设置;}不,但我读,我需要它吗?

公共字符串的firstName {搞定;组; }
公共字符串的lastName {搞定;组; }
公共字符串assocRFID {搞定;组; }
公众诠释assocID {搞定;组; }
公共BOOL canDoDiverts {搞定;组; }
公共BOOL canDoMHE {搞定;组; }
公共BOOL canDoLoading {搞定;组; }
}

公共Form1中()
{
的InitializeComponent();
}

私人无效buttonAddAssoc_Click(对象发件人,EventArgs五)
{
#地区的Datagrid创建 - 名称:DT
DataTable的DT =新的DataTable ();
dt.Columns.Add(名);
dt.Columns.Add(姓氏);
dt.Columns.Add(RFID);
dt.Columns.Add(副编号);
dt.Columns.Add(转移(Divert));
dt.Columns.Add(MHE);
dt.Columns.Add(加载);
dataGridAssociates.DataSource = DT;
#endregion

//首先&放大器;姓分路器
串allValue = textBoxAssocName.Text;
串firstNameTemp =的String.Empty;
串lastNameTemp =的String.Empty;
INT getIndexOfSpace = allValue.IndexOf('');

的for(int i = 0; I< allValue.Length;我++)
{
如果(I< getIndexOfSpace)
{
firstNameTemp + = allValue [I]
}
,否则如果(I> getIndexOfSpace)
{
lastNameTemp + = allValue [I]
}
}
firstNameTemp = firstNameTemp.Trim(); //删除空格
lastNameTemp = lastNameTemp.Trim(); //要删除空空间
//结束分路器

INT assocIDTemp; //成功的TryParse
布尔assocIDparse; //布尔为的TryParse

//尝试解析关联ID为整数
//包括捕 - >返回
assocIDparse = int.TryParse(textBoxAssocID.Text,出assocIDTemp);
如果(assocIDparse ==假)
{
MessageBox.Show(请在AssocID输入只使用数字);
的回报;
}
变种associateList =新的List<&副GT;();
associateList.Add(新副教授
{
的firstName = firstNameTemp,
lastName的= lastNameTemp,
assocID = assocIDTemp,
canDoDiverts = checkBoxDiverts.Checked,
canDoMHE = checkBoxMHE.Checked,
canDoLoading = checkBoxLoading.Checked,
});
textBoxAssocID.Clear();
textBoxAssocName.Clear();
textBoxRFID.Clear();
}
}
}


解决方案

我不会说这是这样做的,但与WinForms和可以使用的BindingSource控制要做到这一点DataGridView控件最优雅的方式。我已经添加下面的示例与修改后的代码来实现你正在尝试做的。



要点,这里要注意如下:




  • 请注意这是在顶部创建的BindingSource实例。这是我用来绑定到DataGridView在构造

  • 在构造函数中的元素,我附上的准列表的实例到BindingSource的DataSource

  • 在构造函数中,我再次BindingSource的连接到DataGridView控件的数据源。

  • 最后在按钮事件的底部,我首先创建关联的一个实例在BindingSource的使用的AddNew(这将在Asociate列表实例自动创建在构造函数创建),并赋予其相应的值,它然后将它们直接在DataGridView填充。



任何问题,让我知道

 只读的BindingSource _bindingSource =新的BindingSource(); 

公共Form1中()
{
的InitializeComponent();
_bindingSource.DataSource =新的List<&副GT;();
dataGridAssociates.DataSource = _bindingSource;
}

公共类大专
{
公共字符串的firstName {搞定;组; }
公共字符串的lastName {搞定;组; }
公共字符串assocRFID {搞定;组; }
公众诠释assocID {搞定;组; }
公共BOOL canDoDiverts {搞定;组; }
公共BOOL canDoMHE {搞定;组; }
公共BOOL canDoLoading {搞定;组; }
}

私人无效buttonAddAssoc_Click(对象发件人,EventArgs五)
{
//首先&放大器;姓分路器
串allValue = textBoxAssocName.Text;
串firstNameTemp =的String.Empty;
串lastNameTemp =的String.Empty;
INT getIndexOfSpace = allValue.IndexOf('');

的for(int i = 0; I< allValue.Length;我++)
{
如果(I< getIndexOfSpace)
{
firstNameTemp + = allValue [I]
}
,否则如果(I> getIndexOfSpace)
{
lastNameTemp + = allValue [I]
}
}
firstNameTemp = firstNameTemp.Trim(); //删除空格
lastNameTemp = lastNameTemp.Trim(); //要删除空空间
//结束分路器

INT assocIDTemp; //成功的TryParse
布尔assocIDparse; //布尔为的TryParse

//尝试解析关联ID为整数
//包括捕 - >返回
assocIDparse = int.TryParse(textBoxAssocID.Text,出assocIDTemp);
如果(assocIDparse ==假)
{
MessageBox.Show(请在AssocID输入只使用数字);
的回报;
}

VAR OBJ =(副)_bindingSource.AddNew();
如果(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();

}


I'm trying to get "firstName, lastName, assocID, etc." to display in a datagrid on my form. I'm a new programmer/script kiddie, sorry if this is a dumb question. I just don't know how to call associateList.firstName to a readable datagrid entry.

I would like the datagrid to use every associate in associateList if possible. Was considering a basic counter on an index refrence somehow.

Other input on how I'm writing my code is appreciated as well. I'm new, and self-taught.

In short : I want the associates to display in the datagrid using columns to separate the information.

The datagrid name is dataGridAssociates on the windows form.

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();
        }
    }
}

解决方案

I won't say that this is the most elegant way of doing this but with WinForms and DataGridView control you can use the BindingSource control to do this. I have added a sample below with the modified code to achieve what you are trying to do.

The key points to note here are as follow:

  • Notice the BindingSource instance which is created at the top. This is the element that I have used to bind to the DataGridView in the constructor
  • In the constructor, I attach a instance of an Associate list to the DataSource of the BindingSource
  • In the constructor again, I attach the BindingSource to the DataSource of the DataGridView control.
  • Finally at the bottom of the button event, I first create an instance of Associate using AddNew in the BindingSource (this will automatically create in the Asociate list instance create in the constructor) and assign their appropriate values to it which are then populated in the DataGridView directly.

Any problem let me know.

    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();

    }

这篇关于无法弄清楚如何类字符串转换为一个DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆