GridView Initial Editing mode? - asp.net

I am writing a web application that is used for tracking and entering some everyday user data... Every day I need to enter some data for multiple users manually...
I will choose the date with the calendar, and for that date I will get DB values assigned for all users on chosen date...If there are some entries in database for that date i will display it in gridview...
Here is the look of gridview for chosen date...
USERS DATA
------------
User1 Data1
User2 Data2
User3 Data3
But, if I choose the date for which there are no entries in DB, I would like to display the gridview containg User names in each row, but I want the second column (DATA field) to be initially editable for ALL rows (the whole second column should contain textboxes for inserting values, not only selected row as in standard editable gridview mode) allowing inserting of wanted values...
I want it to look something like this
USERS DATA
----------------
User1 TextBox
User2 TextBox
User3 TextBox
My idea is to get Users list from DB and create a datatable with first column containg returned user names, and second column empty... This datatable will be datasource for gridview in which I am going to disable editing mode, and in normal (displaying) mode I am going to have TemplateField containg of textboxes bound to that datatable in second column, and labels displaying usernames in first column... When entering all values there will be SAVE button who is calling a method that will read entered values from datatable and save it to database...
Is there a better way to implement this functionality? If Templatefield textboxes are bound to datatable (as gridview datasource) textboxes entered values are going to be accessible from datable object, but Save button click causes postback, and I don't know how to preserve datatable state? Is there a better way to get wanted gridview with whole second column availibe for inserting new values?
Thank you in advance for your help & time!

Try this
http://www.highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx
http://csharpdotnetfreak.blogspot.com/2009/05/edit-multiple-records-gridview-checkbox.html

I would retrieve the full dataset using a left join in the database, and if there is no entry for a user, that user will be returned with a blank value. Something like this:
select users.user,
isnull(entries.entry,'')
from users
left join entries on users.userid = entries.userid
I think you can override the OnDataBinding (or OnDataBound) method to check and see if the second column is empty, and place the field in edit mode if it is. This way, if some users have entries but some don't, only the users with no entry will be in edit mode. Any user with an entry will be in display mode. There is a sample for this at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx.

Related

SQL Server : sum of 2 columns in each row display

I am using SQL Server. I have 2 columns Passed students and Failed Students in my database.
How to write a query which displays both these columns along with a third column which is Total Students which displays the sum of the entries in the row?
EDIT
It is not allowing me to ask another question.So,posting it here:
I have a hyperlink field in my gridview as below:
[Please refer comment for the code.For some reason it doesn't get posted here.]
Its basically a runId.When I click on this hyperlink I am redirected to a page called RunAnalysis.I want to access the value of the runId which was clicked in this page.
I was thinking of using query string but there is no event as far as I know that is fired on click of the hyperlink.
My question is how do I access the runId value in this page ? Can someone tell me if some event is fired so that I can send a query string.
Thanks.
You haven't given a lot of background information so I am assuming many things in my response.
Here is a simple aggregated result set:
SELECT SUM(Passed) AS [Passed],SUM(Failed) AS [Failed],COUNT(*) AS [TotalStudents]
FROM dbo.Students
Now if there is some grouping you want to do then you would add a GROUP BY clause like so...
I'm creating a new column because I don't know your schema:
SELECT GradeLevel, SUM(Passed) AS [Passed],SUM(Failed) AS [Failed],COUNT(*) AS [TotalStudents]
FROM dbo.Students
GROUP BY GradeLevel
ORDER BY GradeLevel

How to persist an ASP:Table with TextBoxes inside cells?

I have an ASP.Table whose cells are containing textboxes filled by the user.
When he clicks on a button, I want to store textboxes content into a database but in the onclick event, the table is empty. How can I persist the whole table, and especially the textboxes data ?
Create a name value collection corresponding to the columns and rows of the table. Store each cell value as a part of the name value collection in the DB with the name in the form of r1c1, r1c2, etc.

Maintaining Row Selection in ASP.NET GridView Control

The Setup:
I currently have a page with a GridView control on it inside of an update panel, using a SqlDataSource. I have a timer setup to update the GridView every X amount of seconds. Typically for what I am testing every time the GridView updates about 4-5 new rows of data are added to the gridview, while the last 4-5 get tossed out. I am only displaying 15 results at a time and will have new results coming in every update.
The Problem:
I allow the user to select the rows, while the GridView is being updated. I am handling this by setting the SelectedIndex property. However, when I select a row and then the grid is updated the row the user selected gets pushed down about 4-5 rows and the data in the previous selected index is selected instead. So where they clicked is selected at this point, not what they clicked.
I need a way to determine, if possible from the SqlDataSource/Gridview, how many new rows have been added to the gridview. OR a way to maintain the selected data by the data in the row and not just the SelectedIndex.
Any help is appreciated, thanks.
RESOLVED:
Ok I went ahead and added a new invisible column to my grid, and am now keep track of the unique ID's selected from the DB. By setting an array before databinding, and comparing that to the new array I get after databinding I was able to use a simple Intersect to determine the number of rows that are the same. Then I used that to determine from the total how many are new this postback.
Just an idea:
I think you can use an invisible column (more specifically an ID column) to store the selected rows' IDs value in the Session object and then after the grid updates, you can retrieve this value(s) and select the row(s) again if they are still present.
If you have custom GridView OnRowUpdating event.
public void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Session["CurrIndex"] = GridView.SelectedIndex;//index before insertion
Session["RowCount"] = GridView.Rows.Count;//row count before insertion
//Add new Rows
GridView.SelectedIndex = (Int32)(Session["CurrIndex"]) + ( GridView.Rows.Count - (Int32)(Session["RowCount"]);//update selected index
Session["CurrIndex"] = GridView.SelectedIndex;//restore the index into session
}

Expand/Collapse GridView rows of the same type

I have a gridview with some boundfields and templatefields. My data are sales Unit-Price-Store, right now the user selects a store (from a DropDownList) and the Grid is all Unit-Price for that store.
Is there a way to make put a button next to each unit so that when the user clicks it, that row expands to include a new row for each store? IE, when I'm looking at Store 1 and I click the button I also get to see Unit-Price info for Stores 2,3,4 (but just for this item)?
I used jQuery. Basically, I tagged that column and then did an each statement. If value.innerHTML != Store $(this).closest('tr').hide(); Else assign it a function on click that looks at all 'tr' where Unit matches but Store doesn't and slideToggle()

Limiting gridview row selection in ASP.Net web page

Have a gridview control which will show X amount of rows. I want to the user to be able to select y number of rows, but limit the maximum selected rows to a set view, such as 3. If the user tries to select a fourth row I don't want them to be able to until they unselect a row.
Anyway to do this?
Thanks
Since the GridView works with single selections, how are you allowing them to select the values in the first place, on the client or server? If on the client, you can use JavaScript to do this; simply store an array of the table rows that are selected, if the length is three, then block adding to the array until the user deselects...
Please update how you are doing it, and I can post further.

Resources