views:

219

answers:

2

I am currently doing data binding with a grid view with a data source from an ArrayList. Is there a way to reverse the bind and get the value from the grid view with a one line code?

// Bind to GUI
ArrayList dsList;
gvName.DataSource = dsList;
gvName.DataBind();

// Current Way of getting code from GUI
int iRow = 0;
foreach (GridViewRow gvr in gvName.Rows)
{
    TextBox txtD1 = gvName.FindControl("textboxName") as TextBox;
    if (txtD1 != null) 
    {
        dsList[iRow].D1 = txtD1.Text;
    }
    ....
    iRow++;
}

Is there any way to make this shorter like one liner? Does the API have this?

gvName.ReverseDataBind();
A: 

Well, the real answer is to use a BindingList<T> instead of an array as this takes care of all the two-way communications between the list and the binding so you won't have to manually update the list from the grid.

However, if you can't use a BindingList<T>, the best you can do is retrieve the databound item from the row:

// Bind to GUI
ArrayList dsList;
dgvName.DataSource = dsList;
dgvName.DataBind();

// Slightly simpler way of getting code from GUI
int iRow = 0;
foreach (DataGridViewRow dgvr in gvName.Rows)
{    
    object item = dgvr.DataBoundItem;        
    dsList[iRow].D1 = item.ToString();

    iRow++;
}

The DataBoundItem value returned can be cast to the appropriate type for processing, which is slightly simpler that searching for a text box control.

Dr Herbie
DataBoundItem can only return the first control in the row right?
Nassign
Also the DataGridViewRow is only applicable to windows application and not ASP .NET application which has the GridViewRow object. The GridViewRow object does not have a DataBoundItem.
Nassign
Apologies, I read it as DataGridViewRow and thought it was for Winforms. Perhaps if you put ASP.NET in the title, it might attract more web devs.
Dr Herbie
Thanks Dr Herbie I just changed the title.
Nassign
+2  A: 

No method like that exists in the API.

If you're interested in getting the binding source during the page construction, the best way to do this is to make the source a class-level property of the page itself so that it's available in any of the page's methods. For example:

public class MyPage: Page {
    ArrayList dsList = new ArrayList();

    ArrayList DsList {
        get {
            return this.dsList;
        }
}

If you're interested in getting the binding source on postbacks (in response to client-generated events), that is possible with some qualifications. Presuming you don't want to rebuild the binding source (for example, by re-querying a database, which should always be considered), you'll have to save it somewhere when you first get it so that you can access it later. (databound web controls do not do this themselves.) That somewhere can be a database, but sometimes for convenience, people save it to ViewState or store it in the Session. Both of these approaches have their hazards though and should be used with care - ViewState increases the size of the Html sent to the client and Session consumes server memory until it's explicitly cleared or the session times out.

Jeff Sternal