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?