0

I'm using an SfDataGrid and I've added a context menu to it. The problem I'm having is if the user pulls up the context menu and makes a selection, with no selected rows in the datagrid, the program will fail. So how do I check if a row has been selected in the MenuFlyoutItem_Clicked method?

My context menu is set up like this:

<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>

So far my MenuFlyoutItem_Clicked event looks like this:

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

How can I determine if a row has been selected or even if no rows are selected?

BobG
  • 9
  • 1
  • Do not use string concatenation to create an SQL command. Use parameterized statements. See [why it's a bad idea and how to fix it](//bobby-tables.com). – gunr2171 Aug 29 '23 at 19:30
  • there is an entire section of their docs labeled [Selection](https://help.syncfusion.com/maui/datagrid/selection#getting-selected-rows). I'd guess that you should check if `CurrentRow == null` before you attempt to reference it – Jason Aug 29 '23 at 19:36
  • gunr2171, thank you for that little grammar note. However, I still stand with my question. How can I determine if a row has been selected or even if no rows are selected? – BobG Aug 29 '23 at 19:46

1 Answers1

0
if (PasswdVw.CurrentRow == null)
{
    // nothing selected
    return;
} 
else 
{
    // something selected
}
Jason
  • 86,222
  • 15
  • 131
  • 146