I need to get some data from database and than insert them back just by changing 1 column's values. For example in my database I have A,B,C columns and 5 records. I want to get these 5 rows and change column B's values to some other value and insert them back as new rows (I don't want to update existing rows but I want to add them as new rows. So at the end I'll have total 10 rows in my database).
I'm trying to do that by using a gridcontrol, I don't have any problems with getting the data from database but I just can't figure out how to do what I want.
I tried to do it by using a "for" loop and I was able to get and change column values but I put a gridview.AddNewRow command inside the for loop and this loop only adds one row and always updates that single row. However I want it to do the same changes for all 5 rows and add 5 new row.
Any idea how I can do that ???
Thanks.
Sam HobbsPosted Mar 9, 2010, 12:06 AM
Sam HobbsPosted Mar 8, 2010, 10:41 PM
First I must apologize; I am not very experienced with ASP programming. Apparently ASP programs don't use TableAdapters. So I apologize for asking about a TableAdapter. You say that you are using a TableAdapter, so I am confused again. I will assume there is not a TableAdapter. However you also say you have a BindingSource, which I think also does not apply to ASP. Oh, now I see that Forms can also use a DataGrid, so the following is probably not relevant to you. I will post it anyway.
You can use the data source. For ASP most controls are bound using a SqlDataSource. So the following could be used to put the value of one column for all rows into a textbox. Note that a SqlDataSource also has an Insert method. There are many more things you can do using the SqlDataSource. It would be highly productive to learn about SqlDataSource.
ozzyPosted Mar 8, 2010, 4:48 PM
for (int i =0; i
here I get the data by row number using getrowcellvalue(i,gridview.columns["mycolumn"]....
and than I try to pass it to the new row and I use gridview1.AddNewRow() in this loop.
}
However it only adds one row not 5, and updates this row for each record. At the end I have 6 rows in datagrid but not 10. And I wasn't able to insert that 6th row into database either. So I'm thinking "for" loop may not be the solution. I've been working on this project non-stop and kinda learning things while doing things.
I explained what I exactly want to do on my second post. I have a datagrid bound to a tableadapter, a dataset, a bindingsource with filter. Right now the data I want to process is 5 rows but eventually it will be around 200-250 rows.
Sam HobbsPosted Mar 8, 2010, 4:27 PM
ozzyPosted Mar 8, 2010, 3:17 PM
It's a windows form and yes I meant DataGrid. User actually doesn't interact with DataGrid. But I want the user be able to see the data. Let's say my database table is like this:
ID A B C
1 X 5 3
2 Y 4 2
3 Z 4 1
4 Q 2 1
5 W 2 0
and I want to change it to :
ID A B C
1 X 5 3
2 Y 4 2
3 Z 4 1
4 Q 2 1
5 W 2 0
6 X 3 0
7 Y 2 0
8 Z 1 0
9 Q 1 0
10 W 0 0
As you can see on the sample I get data from 5 rows and insert them as new records. I also get C's values and set them as B column's values and set C to 0 in new records.
Sam HobbsPosted Mar 8, 2010, 2:00 PM
I am not sure of one more thing. What does the user do? How do you determine what records to copy and what data to use in the new records? Is it automatic in the sense that you don't need interaction with the user?