0

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

2 Answers 2

1

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 
}
Sign up to request clarification or add additional context in comments.

Comments

0

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.