الاثنين، 15 يونيو 2015

Working with ListViewDataItem

Add New ListViewDataItem:

var item = new ListViewDataItem(user.UserName, new[] { user.UserId.ToString() });
item.Image = Resources.offline_user;
if (radListView1.InvokeRequired)
                            {
                                radListView1.Invoke(new MethodInvoker(() => radListView1.Items.Add(item)));
                            }
                            else { radListView1.Items.Add(item); }

How to foreach items in radListView:

  foreach (ListViewDataItem item in radListView1.Items)
                        {
                            if (item.SubItems[0].ToString() == member.UserId.ToString())
item.Font = new Font(radListView1.Font, FontStyle.Regular);
                                                item.ForeColor = Color.Black;
                            {

ListViewDataItem MouseClick:

radListView1.ItemMouseClick += radListView1_ItemMouseClick;

private void radListView1_ItemMouseClick(object sender, ListViewItemEventArgs e)
        {
            if (radListView1 != null && radListView1.Items != null && radListView1.Items.Count > 0
                && radListView1.CurrentItem != null)
            {
               
                    //get userID
                    ListViewDataItem item = radListView1.CurrentItem;
                    int seletedUserId = Convert.ToInt32(item.SubItems[0]);
            }
       }

Filter in RadGridView




        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            #region MyRegion

            if (!string.IsNullOrEmpty(txtFilter.Text.Trim()))           
                FilterMethod(txtFilter.Text.Trim(), "ItemName");           
            else
                grdvUsersGroups.Columns["ItemName"].FilterDescriptor = null;
            #endregion
        }   

--------------------------------


private void FilterMethod(string text,string colName)
        {
            var filter1 = new FilterDescriptor {Operator = FilterOperator.Contains, Value = text, IsFilterEditor = true};
            for (var i = 0; i < grdvUsersGroups.ColumnCount; i++)
            {
                if (grdvUsersGroups.Columns[colName].DataType == typeof(string))
                    grdvUsersGroups.Columns[colName].FilterDescriptor = filter1;               
            }
            //grdvUsersGroups.TableElement.ScrollToRow(grdvUsersGroups.Rows.First());

        }
------------------------------------------
To Display Users of Groups Only

private void radioAll_CheckStateChanged(object sender, EventArgs e)
        {
            if (sender != null)
            {
                var rdoButton = sender as RadRadioButton;
                if (rdoButton != null && rdoButton.IsChecked)
                    if (rdoButton.Name == "radioUsers")
                    {
                        FilterMethod("2", "isGroup");
                    }
                    else if (rdoButton.Name == "rdioGroups")
                    {
                        FilterMethod("1", "isGroup");
                    }
                    else
                    {
                        grdvUsersGroups.Columns["ItemName"].FilterDescriptor = null;
                        grdvUsersGroups.Columns["isGroup"].FilterDescriptor = null;
                        if (!string.IsNullOrEmpty(txtFilter.Text.Trim()))
                        {
                            FilterMethod(txtFilter.Text.Trim(), "ItemName");
                        }
                    }
            }

        }     

------------------------------
Font is Regular for User but Bold for Group:

private void grdvUsersGroups_ViewRowFormatting(object sender, RowFormattingEventArgs e)
{
         if (e.RowElement == null || e.RowElement.RowInfo.Index <= -1) return;
         if (e.RowElement.RowInfo.Cells["GroupId"].Value != null)
         {
             e.RowElement.Font = Convert.ToInt32(e.RowElement.RowInfo.Cells["GroupId"].Value) != 0
                                 ? new Font(Font, FontStyle.Bold)
                                : new Font(Font, FontStyle.Regular);                       
         }
         else e.RowElement.Font = new Font(Font, FontStyle.Regular);

}

Get users does not exist in list of users

List<int> userIds = _excludedUsers.Select(s => s.UserId).ToList();
ICollection<UsersGroupsResult> _usersGroupsResult = _usersGroupsResult.Where(p => !userIds.Contains(p.UserId)).ToList();


// Best Practice

_usersGroupsResult = (from c in _usersGroupsResult
                                where !(from o in _excludedUsers 
                                select o.UserId).Contains(c.UserId)
                                select c).ToList();

Add contextMenu for CellElement.


private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            if (e.ContextMenuProvider is GridDataCellElement)
            {
                // this context menu has been fired from the cell
                e.ContextMenu.Items.Clear();
                var cell = radGridView1.CurrentCell;
                if (cell != null && cell.ColumnInfo.Name == "Comment" && cell.Value != null)
                {
                    var menuItemCopy = new RadMenuItem("Copy");
                    menuItemCopy.Image = Resources.copy;
                    menuItemCopy.Click += radMenuItemCopy_Click;
                    e.ContextMenu.Items.Add(menuItemCopy);
                }
            }
        }

Apply new font for your application .



  [DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
            IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

        private static PrivateFontCollection MYpfc = new PrivateFontCollection();

        public static void LoadFont()
        {
            try
            {
                unsafe
                {
                    fixed (byte* pFontData = Properties.Resources.CoText_Arabic_Bd)
                    {
                        uint dummy = 0;
                        MYpfc.AddMemoryFont((IntPtr)pFontData, Properties.Resources.CoText_Arabic_Bd.Length);
                        AddFontMemResourceEx((IntPtr)pFontData, (uint)Properties.Resources.CoText_Arabic_Bd.Length, IntPtr.Zero, ref dummy);
                    }
                    fixed (byte* pFontData = Properties.Resources.CoText_Arabic_Lt)
                    {
                        uint dummy = 0;
                        MYpfc.AddMemoryFont((IntPtr)pFontData, Properties.Resources.CoText_Arabic_Lt.Length);
                        AddFontMemResourceEx((IntPtr)pFontData, (uint)Properties.Resources.CoText_Arabic_Lt.Length, IntPtr.Zero, ref dummy);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Font does not appear correctly ");

            }
        }

Fill ComboBox in RadGridView by Dynamic Class - show/hide column based on value

Class :

public class UserCategory
    {
        public string CategotyId { get; set; }
        public string Category { get; set; }

        public static List<UserCategory> GetAllUserCategory()
        {
            var userCategory = new List<UserCategory>
            {               
                new UserCategory {CategotyId = "1", Category = "Normal"},
                new UserCategory {CategotyId = "2", Category = "Document Control"},
                new UserCategory {CategotyId = "3", Category = "Focal Point"},
                new UserCategory {CategotyId = "4", Category = "Administrator"},
            };
            return userCategory;
        }
    }

Code Behind:

var colCategory = ((GridViewComboBoxColumn)radGridView1.Columns["colCategory"]);
                colCategory.DataSource =xDB.Management. UserCategory.GetAllUserCategory();
                colCategory.ValueMember = "CategotyId";
                colCategory.DisplayMember = "Category";
                colCategory.FieldName = "UserCategory";
if (MyParameters.userProfile.UserCategory != "1")
                {
                    #region MyRegion
                    radGridView1.AllowEditRow = false;
                    if (_isDelegation) btnSave.Text = "Delegate"; else btnSave.Text = "Save Changes";
                    radGridView1.Columns["colDeparments"].IsVisible = false;
                    radGridView1.Columns["colDeparments"].VisibleInColumnChooser = false;
                    #endregion

                }
                else
                {
                    #region MyRegion

                    if (_isDelegation) btnSave.Text = "Delegate";
                    radGridView1.Columns["colDeparments"].IsVisible = true;
                    radGridView1.Columns["colDeparments"].VisibleInColumnChooser = true;
                    #endregion

                }