I have a table that looks like this:
Name Hours
Mark 10
Mark 15
Mark 10
Mark 13
Albert 11
Albert 12
Max 10
Max 13
This data resides in a datatable, which is the datasource for my gridview. I would like to, if possible, iterate through the datatable and insert a row when the Name field changes. Like this:
Name Hours
Mark 10
Mark 15
Mark 10
Mark 13
Albert 11
Albert 12
Max 10
Max 13
Is it possible to use a foreach loop to iterate through this datatable and upon the name changing, insert a row at that point? Or, is there another method to get the desired result?
Thanks for any pointers!
EDIT: My code, so far:
var counter = 0;
var holdName = "";
// looping through datatable dt.
foreach (DataRow row in dt.Rows)
{
//compare name from dt to hold name
//if different
// insert blank row
// change hold name to new name
//continue looping
}
gridview1.Datasource = dt;
gridview1.DataBind();
The problem, though is that setting the hold name within the loop is proving tricky. It's like it needs to be set 'ouside' the loop, first, and then checked within the loop. The problem is, how do I know what the name is without getting the first record in the datatable? Perhaps, that is my answer? Get the first record, put the name in the hold name variable, then start the foreach loop?
I think you will get better results if you use the ASP.NET ListView control and use data groupings, like this:
Using ASP.NET 3.5's ListView and DataPager Controls: Grouping By a Data Field
It is a slightly dated article, but I think it will get you to where you want to go.
As Brad M already stated, a foreach loop will work. You just need to store the name in a variable and check if the name of the current row is the same as the name in the last row (or you could check if the name in the current row is the same as the name in the next row. But this could lead to a OutOfBoundsException).
Related
I am looking into adding new values to an existing session. That session will have values in it and I hope that when I add these new ones into it, it would not remove whatever values that is already in that session. E.g. Before adding, the session has 1 value in it. After adding, it should show 2 values in it.
This is what I have tried:
List<string> toAdd = Session["SendData"] as List<string> ??new List<string>();
toAdd.Add(TxtData.Text.Trim());
Session["behzadList"] = behzadList;
I know that there is already some solutions regarding to this problem( the code above is 1 of them) However, when I tried this code, whatever values that were already in the session is being removed and the new values that I have just add would appear instead. So I am not too sure is there any other ways to solve this problem.
Thanks in advance.
List<string> toAdd = Session["SendData"] as List<string> ??new List<string>();
toAdd.Add(TxtData.Text.Trim());
Session["SendData"] = toAdd; // this was wrong it seems. You dont add to the same
session
So, I managed to solve it myself. I used 3 datatables, 1 to store the session values, 1 to store the values that I am going to add in. The last one, is used to merge both datatables values together.
Reason why so many datatables is actually based on your needs and your functions.
For me, it is actually not to confuse the user. I previously only used only 1 datatable. The steps are almost the same as the one I am showing below, but only difference is that you don need to do merging. However, when I add in the new values, it will display not just the values added, but also the values that were already in the session.
This is what I have changed:
//First store session values into the first datatable
DataTable dt = (DataTable)Session["SendData"]
//Create a second datatable to store the values you are going to add in
Datatable DT = new DataTable();
// Do whatever you need to fill this datatable with the values that are going to be added in.
//E.g. add columns, then use datarow to add into datatable or even sql query then fill etc.
//After that create a third datatable to do the merging
DataTable DT3 = new DataTable();
//At this point is where you will merge 2 datatables together.
DT3 = dt.Copy();
DT3.Merge(DT);
//The session will linked be the third datatable instead as it has all the values that you want
Session["SendData"] = DT3;
It kinda looks lengthly but I have to do the explaination as it can be confusing. You could possibly change some things here and there depending on what you want to do with the code. To anyone that may need this for some reference, hope this helps.
To the people that help me by commenting on this post, thanks for giving me suggestions.
I have a HTML input box that I want to be able to submit URLs into. Only problem is that when I go to submit multiple urls, they are inserted into 1 row in the SQLite database in one big blob.
"http://google.com/http://stack.com/http://aol.com/"
When I want them to be:
Row 1: http://google.com/
Row 2: http://stack.com/
Row 3: http://aol.com/
Essentially, for every URL in the input box to be put into it's own individual row.
How can I achieve this?
This can be done in two steps.
Split the URL(consider it as a string) on the basis of a token ('/').
Then loop over the split string (as per example 3) and save it in DB.
Here is the example code.
var str = 'http://google.com/http://stack.com/http://aol.com/';
var str_array = str.split('/');
I have a DataGrid, with around 100 differnt entries, I am wanting to loop through each row, and get a field form the database(the field is URL), into a string.Basically I have the code to do a screen scrape using the HTMLAgilityPack, but I want to do this for each row in the database, and update the database based upon the returned screen scrape.
Loop through each row
Store Each URL into a String
Use HtmlAgilityPack to do a screen scrape done
Update Database field "Price" with a value returned from step 3
You can loop through all the rows in your datagrid and add the value of a specific column to a list of strings.
var hyperlinks = new List<string>();
var indexOfColumn = 2;
foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
hyperlinks.Add(((DataGridViewCheckBoxCell)row.Cells[indexOfColumn]).Value);
}
Then use the hyperlinks list and update the database as you require in step 4.
I would say you could update the database inside the foreach statement and not bother adding to the list of hyperlinks, but for 100 rows that could be quite costly and unless you've got batching or something similar so you actually only hit the database every 20 updates or so.
I have column in database
ID From To s1from s2to s2price fare
1 Delhi Manali 17-Dec-2010 19-Dec-2010 $900 $600
2 USA Canada 18-Dec-2010 20-Dec-2010 $500 $800
3 Newyork salinas 19-Dec-2010 22-Dec-2010 $760 $1000
I want when any user search For: Delhi to Manali on between 17-Dec-2010 to 19-Dec-2010 then the price would be automatically changes to $900 in gridview else the default price wold be displayed in fare is $600 if he search for DelHi to Manali after 19-Dec-2010.
I m confused how to implement this logic ... using vb.net ...
If I understand correctly, I think you could do using this query:
SELECT DateSpecificFare = CASE WHEN s1from <= inDate AND s2to >= inDate THEN s2price ELSE fare END FROM YourTable WHERE From = inFrom AND To = inTo
Where inDate, inFrom and inTo would be the parameters the user selects.
Your previous query also made it look like you might not know how to make the calls to the database so if that's the case, look at SqlCommand.ExecuteScalar which includes some sample code showing you how to do that. Or if you need to return more than one column and/or record from your table, look at SqlCommand.ExecuteReader instead.
Btw, you should really edit your previous question rather than create a new one, otherwise it might get closed as a duplicate. But in this case I'll answer this one rather than the previous ones since this is the one with the best question.
I have a row list as [10,20,30] i want to pass the selected row list ie if (20 is selected ill pass that 20 as query string and retrieve the value). and also i want to select the page number too. What are the possible ways to achieve the selected value from row list and current page number and then pass the value to query string.
I tried var Page=$(".ui-pg-input").val()
but it is not firing i think i am missing something out here.
I got it done. By using the predefined session values in it