Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表框中的项在文本框中可以有不同的值?_C# - Fatal编程技术网

C# 列表框中的项在文本框中可以有不同的值?

C# 列表框中的项在文本框中可以有不同的值?,c#,C#,我有一个列表框,它显示我从DataGridView中拖动的项目。列表框中的项目显示其菜单代码。我想做的是在文本框中显示列表框中每个项目的菜单。我试图这样做,但菜单代码显示在该文本框上。请看下图 是否可以在该文本框上显示菜单?我已经做了这个代码,但我不认为这是正确的 private void menuDataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) //this code triggers when I

我有一个列表框,它显示我从DataGridView中拖动的项目。列表框中的项目显示其
菜单代码
。我想做的是在文本框中显示列表框中每个项目的
菜单
。我试图这样做,但
菜单代码
显示在该文本框上。请看下图

是否可以在该文本框上显示
菜单
?我已经做了这个代码,但我不认为这是正确的

private void menuDataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) //this code triggers when I dragged an item from datagridview to listbox.
{
    menuDataGrid.DoDragDrop(menuDataGrid.CurrentRow.Cells[3].Value.ToString(), DragDropEffects.Copy);
}

private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{        
    pricetxtbox.Text = menuListBox.SelectedItem.ToString();
}

private void ShowDataToGrid()  //datagrid code
{
    db_connection();

    MySqlDataAdapter datagrid = new MySqlDataAdapter();
    string selectAll = "SELECT * FROM Menu";
    datagrid.SelectCommand = new MySqlCommand(selectAll, connect);

    DataTable tbl = new DataTable();
    datagrid.Fill(tbl);

    BindingSource bs = new BindingSource();
    bs.DataSource = tbl;

    menuDataGrid.DataSource = bs;
    menuDataGrid.Columns[6].Visible = false;
    menuDataGrid.Columns[7].Visible = false;
}

您应该将数据打包到具有要显示和保留的属性的对象类中。例如:

public class MenuItem()
{
    public float MenuPrice { get; set; }
    public string MenuCode { get; set; }
}
如果在ListView中存储MenuItem类型的数据,则可以执行以下操作:

private void menuListBox_SelectedValueChanged(object sender, EventArgs e) textbox
{        
    var item = menu.ListBoxItem.SelectedItem as MenuItem;
    if(item == null) return;

    pricetxtbox.Text = item.MenuPrice;
}

这是一种方法,如果你想增强你的代码,你可能应该考虑为你的文本框使用数据绑定。

< P>你应该把你的数据打包到一个对象类中,你要显示和保存这些属性。例如:

public class MenuItem()
{
    public float MenuPrice { get; set; }
    public string MenuCode { get; set; }
}
如果在ListView中存储MenuItem类型的数据,则可以执行以下操作:

private void menuListBox_SelectedValueChanged(object sender, EventArgs e) textbox
{        
    var item = menu.ListBoxItem.SelectedItem as MenuItem;
    if(item == null) return;

    pricetxtbox.Text = item.MenuPrice;
}

这是一种方法,如果你想增强你的代码,你可能应该考虑为你的文本框使用数据绑定。

< P>我假设MeuBube也出现在不同的格上的GRIDVIEW中,比如<代码> cell [4 ] < /代码> ./P> 然后你可以做这样的事情

private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{        
    foreach (DataGridViewRow row in menuDataGrid.Rows)
    {
        if (row.Cells[3].Value.ToString().Equals(menuListBox.SelectedItem.ToString()))
        {
            pricetxtbox.Text = row.Cells[4].Value.ToString();
            break;
        }
    }
}

我将假设菜单也出现在GridView的另一个单元格中,比如说
单元格[4]

然后你可以做这样的事情

private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{        
    foreach (DataGridViewRow row in menuDataGrid.Rows)
    {
        if (row.Cells[3].Value.ToString().Equals(menuListBox.SelectedItem.ToString()))
        {
            pricetxtbox.Text = row.Cells[4].Value.ToString();
            break;
        }
    }
}

你能发布GridView的代码吗?嗨@MohamedNajiullah我会编辑我的帖子。:)我很抱歉。我还不够清楚。我是说GridView的aspxcode@MohamedNajiullah这是C#windows窗体应用程序。:)我的错。GridView上也有菜单吗?你能发布GridView的代码吗?嗨@MohamedNajiullah我会编辑我的帖子。:)我很抱歉。我还不够清楚。我是说GridView的aspxcode@MohamedNajiullah这是C#windows窗体应用程序。:)我的错。GridView上也有菜单吗?很高兴听到这个消息!很高兴听到这个消息!