views:

278

answers:

3

I have a method like the following in a Utility class. I would like to change the parameter dataSource to accept any type of datasource i.e. dataset, dataview, List, datatable, arraylist.

Is this possible? How would I change the method signature (and parameters and types) to allow me the flexibility of passing in any acceptable datasource for binding?

Thank you

public void FillCombo(DropDownList ddl, DataTable dataSource, string textField, string valueField, bool addSelect) {

    ddl.DataValueField = valueField;
    ddl.DataTextField = textField;
    ddl.DataSource = dataSource;
    ddl.DataBind();

    if (addSelect) AddSelectCombo(ddl, "Select", -1);

}
+2  A: 

I believe the following is what you're looking for.

public void FillCombo<TSource>(DropDownList ddl, TSource dataSource, string textField, string valueField, bool addSelect) {

    ddl.DataValueField = valueField;
    ddl.DataTextField = textField;
    ddl.DataSource = dataSource;
    ddl.DataBind();

    if (addSelect) AddSelectCombo(ddl, "Select", -1);

}
Adam Maras
What is the advantage of using generics in this case? Yes, David asked for them, but I suppose he could write this code himself easily. And it doesn't give him any benefits over accepting an object.
Roman Boiko
I think I read too far into the word "generic" in the title of the question. I think I managed to make a generic solution into a solution with generics :) Either way, it works.
Adam Maras
@Adam: whoops, I also thought that David asked for generics, but after reading your comment I returned to it and found that probably I was wrong. Thanks :)
Roman Boiko
+8  A: 

Well, since the DataSource property on a DropDownList has type object you could change your method signature to accept an object. That would not make your method generic (in the .net sense), but maybe it would be enough anyway.

klausbyskov
+1. Since there is no common interface for all datasources which could be used as a constraint for generics, there is no advantage of using generics in this situation.
Roman Boiko
A: 

I have something similar in my library, and what I did is create it as an Extension method (.NET 3.5+) on the DropDownList class...

Imports WC = System.Web.Controls

...

<Extension()> Public Sub DataBindEx(
    ByVal target As WC.DropDownList, ByVal DataSource As Object, 
    ByVal DataTextField As String, ByVal DataValueField As String,
    Optional ByVal SetTooltips As Boolean = True
)
    With target
        .DataSource = DataSource
        .DataTextField = DataTextField
        .DataValueField = DataValueField
        .DataBind()
    End With

    If SetTooltips Then
        For Each li As ListItem In target.Items
            li.Attributes("title") = li.Text
        Next
    End If
End Sub

I'll leave it for you to translate to C#.

eidylon
Question addresses a different problem.
Roman Boiko
Not really. I mentioned the Extension method, but I am showing how I dealt with a similar case where any type of datasource needs to be accepted. Since the .DataSource property of the DropDownList control is of type Object, that is what I wrapped it to in the parameter-list of my function.
eidylon