如何在C#中获取所选项的索引或ID [英] How do I get the index or id of the selected item in C#

查看:168
本文介绍了如何在C#中获取所选项的索引或ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I wanted to get the Id of the selected item in my List but I don't know how to do it. When the item is selected it turns to IsApplied = true but if not selected it turn to IsApplied = false and save it.





我尝试过:



这是我的代码:





What I have tried:

Here is my code :

public void SetVehicleEquipment(VehicleOptionServiceModel command, MacHaik.Data.IVehicleOption vehicleOption)
        {
            if (command.vOptionEquip != null)
            {
                foreach (var i in command.vOptionEquip)
                {
                    if (i.IsApplied == true) i.IsApplied = true; else i.IsApplied = false;
                }
            }
            if (command.vStandardEquip != null)
            {
                foreach (var c in command.vStandardEquip)
                {
                    foreach (var d in c.Data)
                    {
                        if (d.IsApplied == true) d.IsApplied = true; else d.IsApplied = false;
                    }
                }
            }

            var vopt = _jsonHelper.Serialize(command);
            if (vopt != null)
            {
                var vOption = _vehicleOption.GetVehicleOption(vehicleOption.IVehicleId, vehicleOption.Name);

                if (vOption != null)
                {
                    using (var context = new MacHaikCrmEntities())
                    {
                        vOption.OptionCodes = vopt;
                        context.SaveChanges();
                    }
                }
            }
        }







The problem of the code above is, I havent get the index of the item selected..

推荐答案

问题是列表没有任何选择的概念,因此它没有选定项目 - 所以你不能得到一个索引,因为它没有。



大多数数据感知控件都了解选择 - DataGridView具有SelectedCells,SelectedColumns,SelectedRows和SelectionMode属性,例如: DataGridView类(System.Windows.Forms)| Microsoft Docs [ ^ ]以及基于选择的事件 - 以及您可能希望用于选择项目的其他控件,或者项目将具有类似的属性和事件。



但是我们不知道您的应用程序使用什么控件来与用户交互 - 我们甚至不知道您的应用程序在哪种环境下工作!



首先从用户界面开始,看看用户在选择什么!
The problem is that a List doesn't have any concept of "selection", hence it doesn't have a "selected item" - so you can't get an Index as it doesn't have one.

Most "data aware" controls understand selection - DataGridView has the SelectedCells, SelectedColumns, SelectedRows, and SelectionMode properties for example: DataGridView Class (System.Windows.Forms) | Microsoft Docs[^] as well as Selection based Events - and other controls which you might expect to be used to choose an item, or items will have similar properties and events.

But we have no idea what controls your app uses to interface with the user - heck we don't even know what environment your app is working in!

So start with the user interface and look at what the user is selecting from!


这篇关于如何在C#中获取所选项的索引或ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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