c#, web forms, vs2003
i have this data grid view that im trying to create a shopping cart.
my problem is when i data bind to each and every time if ther is previous binding that values still there how do i only bind the new value and display only new value in a data grid .

Recommended Answers

All 17 Replies

post your code here then after any one will able to help u generel senario is at every binding first cleare your data gride view but code forst asap

every time the part in [COLOR="Red"]red[/COLOR] executes the datagrid will have the privious record with it too

string[] selectedbooks;
    static DataSet dsSort;
    static int loadcount= 0;

    static int numberDiv=0;
    protected System.Web.UI.WebControls.DataGrid dgresults;
    protected System.Web.UI.WebControls.Label lblno;

    localhost.WebService webS=null;


    private void Page_Load(object sender, System.EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //loadcount=0;
        webS= new localhost.WebService();

        if (Page.IsPostBack)
        {

            if (Session["addcount"]==null)
            {
                lblno.Visible=true;
                lblno.Text="You Haven’t selected any book please vist our books page and select books you want to buy. ";



            }
            else
            {


            }
        }
        else
        {
            if (Session["addcount"]==null)
            {
                lblno.Visible=true;
                lblno.Text="You Haven’t selected any book please vist our books page and select books you want to buy. ";



            }
            else
            {
                //int num=dgresults.Items.Count;
                //if(num==1)

                if (Convert.ToInt32(Session["loadcount"])==2)
                {



                    totalcal();
                }
                else
                {

                selectedbooks= new string[100];
                    string st=Session["addcount"].ToString();
                    selectedbooks=(string[])Session["selbooks"];

                    txtun.Text=st;

                    DataSet ds=webS.GetBookData(selectedbooks,Convert.ToInt32(Session["addcount"]));
                    dsSort=ds;
                    dgresults.DataSource=ds;
                    dgresults.Visible=true;
                    dgresults.Enabled=true;
                    dgresults.DataBind();
                    loadcount++;
                    Session["loadcount"]=2;


                       totalcal();

                }






            }
        }
    }

Just a quick note... nothing against your requests but...

1) please phrase your post titles to give some idea of the question contained
2) please please PLEASE stop adding a line of periods at the end of every single post title you create :)

Having two identically titled posts about different problems you're having (even if they're related to the same project) is confusing to say the least and makes it look like a duplicate post at first glance through the post titles.

sorry about that

No worries :) Just pointing it out for future reference.

try this code will help

if (this.dataGridView1.DataSource != null)
{
     this.dataGridView1.DataSource = null;
}
else
{
    this.dataGridView1.Rows.Clear();
}

but my data gird view dosen't have a this.dataGridView1.Rows.Clear();

insted i tried
dgresults.Columns.Clear(); but then the data want show at all

I believe what he's getting at is for you to put your own datagridview's name where his sample has dataGridView1.

As such:

if (this.dgresults.DataSource != null)
{
     this.dgresults.DataSource = null;
}
else
{
    this.dgresults.Rows.Clear();
}

ya i know but im using vs 2003 but my data grid view dosen't give any Rows option it only gives Items but that dosen't have a Clear method

i get this error when i use it like that

'System.Web.UI.WebControls.DataGrid' does not contain a definition for 'Rows'

i get this error when i do it like that

'System.Web.UI.WebControls.DataGrid' does not contain a definition for 'Rows'

So there's no dgresults.Items.Clear()?

Hmm... I was hoping not to have to open my VS for this one LoL give me a few min.

Oh ho.... dataGrid, not dataGridView...

That's why the suggestions given above don't work (Rows.Clear()) because it's a dataGrid and not a dataGridView so they have different properties.

You might have some success with .Items.Clear()... might not, doing some research on it for you at the moment.

I have to admit I'm drawing blanks on how to clear the items within a dataGrid. That construct was replaced by dataGridView many years ago and documentation (useful documentation) on manipulation of dataGrid items is hard to find.

Honestly, if you're using VS 2003 it might be time for an upgrade since we're now dealing with a 7 year gap in technology there.

Sorry I couldn't be more help :(

nahh thats ok thanks any way

any way guys i solved it my self

it was the data set in side my web service i have declared it static so every time previous data was there

thanks any way

Glad you sorted it out :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.