Giter Club home page Giter Club logo

Comments (30)

davidegironi avatar davidegironi commented on May 21, 2024

Thank you for sharing this solution, It could helps other people that are in trouble with this!
If it's not a problem for you I would add this discussion link to the README of the project...

from advanceddatagridview.

OceanAirdrop avatar OceanAirdrop commented on May 21, 2024

Oh, yes, that's fine. No probs from me.

from advanceddatagridview.

davidegironi avatar davidegironi commented on May 21, 2024

README file has now been updated. Thank you!

from advanceddatagridview.

Tommixoft avatar Tommixoft commented on May 21, 2024

This example doesn't work for fields that are boolean type. Boolean type dont have brackets in filter text and filtering crashes because of out of index when spliting and using index 1 which dont exist.

from advanceddatagridview.

OceanAirdrop avatar OceanAirdrop commented on May 21, 2024

Ahh yes... For my use case I didn't have any boolean fields in my data model. I must admit, I didn't check all datatypes. Only the ones I needed.. But it shouldn't be too hard to add that in though. If I have got time I will take a look tomorrow.

from advanceddatagridview.

Tommixoft avatar Tommixoft commented on May 21, 2024

from advanceddatagridview.

OceanAirdrop avatar OceanAirdrop commented on May 21, 2024

ahh cool.. yeah, that is pretty much what I would probably have done as well..

from advanceddatagridview.

OceanAirdrop avatar OceanAirdrop commented on May 21, 2024

I have just knocked up a proof of concept demo app for this which can be found here: https://github.com/OceanAirdrop/AdvancedDataGridViewDataModel

I have tested it with the following data types:

  • int
  • string
  • bool
  • DateTime
  • double
  • decimal

If there are any other missing column types, I will leave it as an exercise for someone to add / improve upon.

from advanceddatagridview.

Tommixoft avatar Tommixoft commented on May 21, 2024

from advanceddatagridview.

Tommixoft avatar Tommixoft commented on May 21, 2024

from advanceddatagridview.

davidegironi avatar davidegironi commented on May 21, 2024

@Tommixoft it can be done, where can i find your mod? here https://github.com/OceanAirdrop/AdvancedDataGridViewDataModel ?

from advanceddatagridview.

desarrollo03TR avatar desarrollo03TR commented on May 21, 2024

My implementation:

private void dataGridView2_FilterStringChanged(object sender, Zuby.ADGV.AdvancedDataGridView.FilterEventArgs e)
{
    try
    {
        if ( string.IsNullOrEmpty(dataGridView2.FilterString))
        {
            m_filteredList = m_dataGridBindingList;                
        }
        else
        {
            m_filteredList  = FilterAndSortDataStr(m_dataGridBindingList, dataGridView2.FilterString, dataGridView2.SortString);
        }
        dataGridView2.DataSource = m_dataGridBindingList;
    }
    catch (Exception ex)
    {
        Log.Error(ex, MethodBase.GetCurrentMethod().Name);
    }
}

FilterAndSortDataStr:

private List<T> FilterAndSortDataStr(IEnumerable<T> collection, string filter, string sort)
{
    if (collection == null) {
        return new List<T>();
    }

    if (string.IsNullOrWhiteSpace(filter) && string.IsNullOrWhiteSpace(sort)) {
        return collection.ToList();
    }

    var table = new DataTable {
        CaseSensitive = false
    };

    var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(prop => !Attribute.IsDefined(prop, typeof(IgnoreDataMemberAttribute)))
        .ToList();
    
    table.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
    
    table.Columns.Add(new DataColumn { DataType = typeof(int) });

    var itemList = collection.ToArray();
    var count = itemList.Length;
    for (var i = 0; i < count; i++) {
        var data = new object[props.Count + 1];
        var dataItems = props.Select(p => p.GetValue(itemList[i], null)).ToArray();

        for (var z = 0; z < props.Count; z++) {
            data[z] = dataItems[z];
        }

        data[props.Count] = i;
        table.Rows.Add(data);
    }

    DataRow[] rows = null;

    try {
        var dv = table.DefaultView;
        dv.RowFilter = filter ?? string.Empty;
        dv.Sort = sort ?? string.Empty;
        rows = dv.ToTable().Rows.Cast<DataRow>().ToArray();
    }
    catch(EvaluateException) { }

    var result = new List<T>();
    if (rows != null) {
        var indexes = rows.Select(r => (int)r[table.Columns.Count - 1]).ToArray();

        for (var i = 0; i < count; i++) {
             if (indexes.Contains(i)) {
                result.Add(itemList[i]);
            }
        }
    }

    return result;
}

FilterAndSortDataStr converts the source list to DataTable and apply the filter and sort and then it convert back to list. Maybe not the best performance, but it works with every filter/sort.

from advanceddatagridview.

OceanAirdrop avatar OceanAirdrop commented on May 21, 2024

I like it. Not tested it out, but this is much cleaner than my original proof-of-concept and because it uses reflection, it side-steps the issue of having to detect the column type.

Nice....

from advanceddatagridview.

RIDAAbder avatar RIDAAbder commented on May 21, 2024

hello i charged all the libraries in my project but it seems that my visual studio doesn't recognisez DataPointGridViewModel
some help please

from advanceddatagridview.

OceanAirdrop avatar OceanAirdrop commented on May 21, 2024

Hi @RIDAAbder - Yyou need to replace DataPointGridViewModel with whatever data class you have. That's the data you want to display in the grid.

For example:

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Hi ,
i have downloaded your demo project , thanks for developing advancedgrid with objects , after filtering i am not see other entries in the filter form like excel filter.

Could you please help me in this.

Regards,
Ravindra

from advanceddatagridview.

davidegironi avatar davidegironi commented on May 21, 2024

Dear @ravit23 , please test it with the AdvancedDataGridViewSample solution provided.

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Thanks for your reply , please find the attached document for your referrance, i am not able to see complete entries in the filter popup window once i selected any item in the filter.
Advanced data Grid View Filter.docx

from advanceddatagridview.

RIDAAbder avatar RIDAAbder commented on May 21, 2024

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Hello,
Thanks for quick response.
Based on your demo project code only i am trying , i am not able to see complete entries in filter popup window once i filtered , Please find my below steps as per the excel

  • useually if we filter in any one column for one entry the respective rows will be displayed in the excel sheet
  • after that if i want add another filter on top of my first filter then i will open filter pop up and will select different entry. this is not happening in this demo project.
    please see attchement in my previous comment there i add all the screenshots with selected DataPointId column.

Regards,
Ravindra

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Please let me know if my query is not clear.

from advanceddatagridview.

davidegironi avatar davidegironi commented on May 21, 2024

Thanks for your reply , please find the attached document for your referrance, i am not able to see complete entries in the filter popup window once i selected any item in the filter.
Advanced data Grid View Filter.docx

You question is not referred to the topic of this issue. Anyway, you should check the SetTextFilterRemoveNodesOnSearch method, i think that's the behaviour you want to achieve.

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Thanks for the reply, even this method is not triggering is not called during this operation. Finally i have achieved this by IBindingListView but i am facing Empty rows issues in the datagrid view. Please find the below code snippet which i am facing empty rows issue.
public object this[int index]
{
get
{
if (index < AllMessages.Count)
{
return AllMessages[GridMessages[index]];
}
return string.Empty;
}

        set
        {
            //throw new NotImplementedException();
        }
    }

Since you have explained IBindinglist in another blog so i am asking here, during initial load allmessages will be assigned to datagrid , after that based on filter gridmessges will hold the filtered ID's and will evaluate this method, due to this AdvancedDataGridView showing empty rows after filtered rows display..

Thanks in Advance..
Ravindra

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Please suggest me how to resolve this empty rows issue in the AdvancedDataGridView.

from advanceddatagridview.

ravit23 avatar ravit23 commented on May 21, 2024

Hi,
i am able to resolve all the issues , no need to look for any thing, thanks for quick responses for my queries.

Regards,
Ravindra

from advanceddatagridview.

davidegironi avatar davidegironi commented on May 21, 2024

@ravit23 happy to hear this. It's way better than us solving for you, I mean that you have learn much more in this way.

from advanceddatagridview.

widavies avatar widavies commented on May 21, 2024

It doesn't look these methods work for custom filter? Does anyone have it working with that?

from advanceddatagridview.

Shirazbello avatar Shirazbello commented on May 21, 2024

Dear @OceanAirdrop
Following through with the solution provided, I am unable to Filter Strings having parenthesis in them.
Strings like Hello without a parenthesis filter without any issue. But a string like Hello(sir) does show an empty grid. It is unable to filter. To solve the issue I removed
// get rid of all the parenthesis
filter = filter.Replace("(", "").Replace(")", "");
from the String Converter Method and I am now getting Syntax Error message when trying to use the filter.
I don't know if it has anything to do with Linq.dynamic library.
Please any suggestions/fixes on how to filter the string with parenthesis. Thanks.

from advanceddatagridview.

dyeigo007 avatar dyeigo007 commented on May 21, 2024

estimado @OceanAirdrop, @davidegironi
buena noche a alguien le sucede que inicia el programa y este se reinicia solo, me esta sucediendo cuando utilizo este componente la primera vez reinicia el programa.

dear @OceanAirdrop, @davidegironi
Good night, it happens to someone that he starts the program and it restarts by itself, it is happening to me when I use this component the first time it restarts the program.

from advanceddatagridview.

vohaha7 avatar vohaha7 commented on May 21, 2024

I don't understand how can it be, that such a popular control, such a popular common problem solution, so bad documented is.
Ok, after some hours of search, have found a solution that perfectly works for me.

  1. One need a DataTable and a underling DataView, coz this class implements IBindingListView which is needed for a filtering/sorting to work. One more time. If your source-class, from which you build a BindingSource doesn't implement this interface, you won't be able to sort or filter. https://stackoverflow.com/a/18534352
  2. Solution: https://stackoverflow.com/questions/3839022/listt-to-dataview and then:
var dataView = Helpers.BuildDataTable<YourClass>(dataList).DefaultView;
advancedDataGridView1.DataSource = dataView;

FilterStringChanged event:

private void advancedDataGridView1_FilterStringChanged(object sender, Zuby.ADGV.AdvancedDataGridView.FilterEventArgs e)
        {
            var grid = (Zuby.ADGV.AdvancedDataGridView)sender;
            var dw = (DataView)grid.DataSource;
            dw.RowFilter = e.FilterString;
        }

Thats it!

UPD 1: In order to convert a class, that contains Nullable data types you need to change in Function CreateTable following:
tbl.Columns.Add(prop.Name, IsNullableType(prop.PropertyType) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
The Function you also need:

private static bool IsNullableType(Type type)
        {
            return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
        }

UPD 2: If you need a sort functionality just create and bind a BindingSource:

var bs = new BindingSource();
bs.DataSource = dataView;
advancedDataGridView1.DataSource = bs;

after that don't forget to change your FilterStringChanged event:

var bs = (BindingSource)grid.DataSource;
bs.Filter = e.FilterString;

from advanceddatagridview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.