ihorko
ihorko

Reputation: 6945

Read data from SqlDataSource or GridView

I have SqlDataSource and GridView on web form. GridView.DataSourceID = mySqlDataSource.

When I call myGridView.DataBind(), all data successfully bind on the page.

How is it possible to read already got data from mySqlDataSource or myGridView objects as DataTable or DataView? Thanks

Upvotes: 0

Views: 1205

Answers (2)

user1333218
user1333218

Reputation: 9

The data in the gridview can read by using FindControl property of Gridview control. For example, for reading values set in the Checkbox column in the grid.

    for (i = 0; i < GridView1.Rows.Count; i++) 

{

            CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("checkbox1");

            //here code for using value captured in chk 
}

Upvotes: 1

jwheron
jwheron

Reputation: 2562

Provided your data set isn't gigantic, you could store it in the Session, bind your GridView to the Session data, and then re-use the Session data in your other web objects.

Your code would then look something like this (you'll have to forgive any minor inaccuracies -- I'm away from my development box at the moment):

protected override OnInit(object sender, EventArgs e)
{
    base.OnInit();

    if (Page.IsPostback == false)
    {
        SetSessionData();
    }
    //
    // Set your GridView, DataTable, DataView, etc. events here.
    //
}

void SetSessionData();
{
        List<YourDataBoundObject> myDataBoundObject = GetYourDataBoundObject(); // Or Collection<T>, IEnumerable<T>, etc.
        Session["data"] = myDataBoundObject;
}

void YourGridView_Load(object sender, EventArgs e)
{
    BindYourGridView();
}

void BindYourGridView()
{
    YourGridView.DataSource = GetSessionData();
    YourGridView.DataBind();
}

List<YourDataBoundObject> GetSessionData()
{
    return (List<YourDataBoundObject>) Session["data"];
}

void YourDataTable_Load(object sender, EventArgs e)
{
    BindYourDataTable();
}

void BindYourDataTable()
{
    YourDataTable.DataSource = GetSessionData();
    YourDataTable.DataBind();
}

Upvotes: 0

Related Questions