即使未选择任何内容,如何获取数据网格的选定行数?

问题描述 投票:0回答:1

我正在使用 SfDataGrid,并为其添加了上下文菜单。我遇到的问题是,如果用户拉出上下文菜单并进行选择,而数据网格中没有选定的行,则程序将失败。那么如何检查 MenuFlyoutItem_Clicked 方法中是否已选择一行?

我的上下文菜单设置如下:

<FlyoutBase.ContextFlyout>
    <MenuFlyout>
        <MenuFlyoutItem Text="Copy User Name"
                        Clicked="MenuFlyoutItem_Clicked"
                        CommandParameter="UsrName"/>
        <MenuFlyoutItem Text="Copy Password"
                        Clicked="MenuFlyoutItem_Clicked"
                        CommandParameter="Passwd"/>
        <MenuFlyoutItem Text="Copy User Name"
                        Clicked="MenuFlyoutItem_Clicked"
                        CommandParameter="WebSite"/>
    </MenuFlyout>

到目前为止,我的 MenuFlyoutItem_Clicked 事件如下所示:

private void MenuFlyoutItem_Clicked(object sender, EventArgs e)
{
    // Somewhere up here I have to check if a data grid row
    // was selected.

    MenuFlyoutItem mnuItem = sender as MenuFlyoutItem;
    string takeAct = mnuItem.CommandParameter as string;
    string url = (PasswdVw.CurrentRow as PasswrdInfo).PassUrl;
    
    string msg, retVal;
    msg = retVal = "";
    int idno = (PasswdVw.CurrentRow as PasswrdInfo).PassId;

    switch (takeAct)
    {
        case "UsrName":
            using (SqlConnection c = new SqlConnection(App.ConnStr))
            {
                string query = "select usrname from PassWrds where Id = " + idno;
                using (SqlCommand cmd = new SqlCommand(query, c))
                {
                    c.Open();
                    retVal = cmd.ExecuteScalar().ToString();
                    c.Close();

                    Clipboard.SetTextAsync(retVal);
                }
            }

            msg = "User Name copied.";
            break;
        case "Passwd":
            using (SqlConnection c = new SqlConnection(App.ConnStr))
            {
                string query = "select password from PassWrds where Id = " + idno;
                using (SqlCommand cmd = new SqlCommand(query, c))
                {
                    c.Open();
                    retVal = cmd.ExecuteScalar().ToString();
                    c.Close();
                }

                string result = Crypto.Decrypt(App.secretKey, retVal);

                Clipboard.SetTextAsync(result);
            }

            msg = "Password copied.";
            break;
        case "WebSite":
            //Nothing Yet.
            break;
    }
}

如何确定是否已选择某行或者是否未选择任何行?

c# maui contextmenu sfdatagrid
1个回答
0
投票
if (PasswdVw.CurrentRow == null)
{
    // nothing selected
    return;
} 
else 
{
    // something selected
}
© www.soinside.com 2019 - 2024. All rights reserved.