Accessing Data cell value in Gridview - asp.net

I have a gridview containing details of ticket types and prices which the gridview gets from the database. In my Default.aspx.vb file I want to access the price value for a specific ticket but I cant figure out how to do it. My GridView looks like this:
Ticket Prices
price_ID type peak_offpeak cost
1 Adult peak 10.00
2 Adult offpeak 5.00
Any help would be much appreciated, thank you for your time
J

Ah I discovered this is what I needed incase anybody else has the same question:
Dim row As GridViewRow = GridView1.Rows(1)
adult_price = row.Cells(3).Text
J

Related

Change DataGrid cell value in VB6

Good day guys.
I am developing VB6 project and I know it's old but thats the standard in Japan. I am not into datagrid expert guy that can tweak everything inside of it but my problem is that I have data to be displayed in datagridview which they want to change the cell value if the data having value for example :
FRUIT | QTY
apple | 1
orange | 2
So if apple having QTY value of "1" I want to change it into something like star or circle value.
It's a datagrid control(databound) in WinForm App. My datasource is sql server.
Thank you guys in advance.
The technology (VB6) is indeed very old; you can try something like
Sub AddSymbol()
For i = 1 To DataGrid1.Rows
If DataGrid1.Item(i, 2) = 1 Then DataGrid1.Item(i, 2) = "*"
Next i
End Sub
where quantity is stored in the second column of DataGrid1 control. Hope this may help.

Insert a row at certain points in a datatable?

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).

Create dynamic table with Add Update Delete options

I am new to asp.net so I dont know the best approach to deal following scenario.
I want to create a dynamic table like following which perform Add Update Delete -
Player SmartGoal Decision Thinking
ABC 10 10 9
PQR 7 9 10
next time table would be -
Player Decision Learning
ABC 10 5
PQR 7 9
I am using c#
I think we have to create databinding to control and control programmatically.
What control should I use?
What is the best approach for this scenario ?
You should use GridView control.
Or for more advanced and neat stuff you probably want to try [telerik][2] controls (not free)
This all needs to be driven by a database.
Just search for two tutorials to get started: how to create a sql server database, and how to use GridView control.
if you need to display hierachical data you can try to use ListView control:
As per my understanding you are fetching data from DB and binding to some control. I thinks the best approach for this to get the dataset from Database and bind to the gridview. You can find many example on net for bind sql datset on internet. Please let me know for any further clarification.
Thanks
Uttam
One Approach is:
Get the column name from Database and add them to a Label.
like lblColumn1.Text = colname1 & so on. Use loop.
Next step is to get the entries and add them to textbox/label again.
similar coding.
Forget not to use Try/catch for exceptions. You never know which column in DB isn't having values or null values.
For Edit/Add/Delete.
Add buttons before each each record to delete and edit
add button should be on top.
**No need to databind or anything like that.
**Basic knowledge of asp will do.
As per my understanding you are fetching data from DB and binding to some control. I thinks the best approach for this to get the dataset from Database and bind to the gridview. You can find many example on net for bind sql datset on internet. Please let me know for any further clarification.
If possible provide some more details. Here i am placing one example where dynamic table is created at runtime
Private Sub BuildSTX9Header()
Dim dtTemp As New DataTable
Dim dr As DataRow
dtTemp.Columns.Add(Me.GetLocalResourceObject("STXLocationID").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SKU").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SKU Description").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("MED").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("MSFSupportedProduct").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("Infor365 Product").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SupportPlan").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("No.Users").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SAM").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("LocationName").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("SerialNumber").ToString)
dtTemp.Columns.Add(Me.GetLocalResourceObject("Phone").ToString)
For i = 0 To dsData.Tables(0).Rows.Count - 1
dr = dtTemp.NewRow()
dr(0) = dsData.Tables(0).Rows(i)(2)
dr(1) = dsData.Tables(0).Rows(i)(3)
dr(2) = dsData.Tables(0).Rows(i)(4)
dr(3) = dsData.Tables(0).Rows(i)(5)
dr(4) = dsData.Tables(0).Rows(i)(6)
dr(5) = dsData.Tables(0).Rows(i)(7)
dr(6) = dsData.Tables(0).Rows(i)(8)
dr(7) = dsData.Tables(0).Rows(i)(9)
dr(8) = dsData.Tables(0).Rows(i)(10)
dr(9) = dsData.Tables(0).Rows(i)(11)
dr(10) = dsData.Tables(0).Rows(i)(12)
dr(11) = dsData.Tables(0).Rows(i)(13)
dtTemp.Rows.Add(dr)
Next
gvLoadRuntime.DataSource = dtTemp
gvLoadRuntime.DataBind()
ViewState("RowCount") = dtTemp.Rows.Count
ViewState("dvRuntimeData") = dtTemp
Thanks
Uttam

How to update the particular column values of gridview on specific dates using vb.net?

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.

Calculated cells in Telerik RadGrid's GridGroupFooterItem

I'm populating a Telerik RadGrid with data from a DataTable (System.Data.DataTable). As I'm supporting someone else's application, I can't use any other data source or display control.
In my grid I have three columns: let's say they are Widgets Produced (column A), Faulty Widgets (column B) and Faultiness Proportion (column C). The database query provides these for me and does the calculation C = B / A.
I have a totals row (a Telerik GridFooterItem) at the bottom of the grid. In columns A and B Telerik calculates the totals for those columns for me. We can't calculate the correct value for the 'total' of column C from column C alone: we have to populate it with (the sum of B) / (the sum of A).
I've managed to do this by handling the DataBound event of the RadGrid and manually populating the cells in the footer. (I had to catch the GridFooterItem in the ItemCreated event, then put values in it in the DataBound event, after it had automatically calculated the totals for A and B for me.) This feels pretty hacky - maybe there's a better way...?
Anyway, the important bit is this:
My grid is split into groups so I also need to populate column C in the GridGroupFooterItems. I can't get my hacky technique to work in this case: I'm finding the footer cell I want with myGridFooterItem["WidgetsProduced"], but I can't get the group footer cells with myGridGroupFooterItem["WidgetsProduced"] - it just isn't a dictionary.
I've tried using myGridGroupFooterItem.Cells[], but this TableCellCollection contains a couple more cells than I'd expect, so accessing them by integer index feels a tad ropey (especially as this is a user-defined report, so the columns may be in any order).
So: how do I populate these cells with my calculation?
your item databound event looks something like
grd_ItemDataBound(object sender,GridItemEventArgs e)
{
//catch the footer element
if(e.Item.ItemType==GridItemType.GroupFooter)
{
(e.Item.FindControl("yourTextBox") as TextBox).Text = your calculated value
}
}
I double-checked whether unique name indexer works with Q2 2009 and Q3 2009 version of RadGrid for ASP.NET AJAX and it worked on my machine. Check your version and ask for more help using the Telerik forums if needed.
Dick
Try this
In Aspx page:
<telerik:GridBoundColumn Aggregate="Sum" DataField="Price" DataFormatString="{0:C}" EmptyDataText="0" FooterText="Total :" HeaderText="Price" UniqueName="Price"> ...
(Or)
In Code behind page:
Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
Dim Item As GridDataItem
Dim value as Double
Select Case (e.Item.ItemType)
Case Telerik.Web.UI.GridItemType.AlternatingItem, Telerik.Web.UI.GridItemType.EditItem,Telerik.Web.UI.GridItemType.Item,Telerik.Web.UI.GridItemType.SelectedItem
Item = e.Item
'------ Calculate Total amount -----------
Item("TotalPayment").Text = CDbl(Item("TotalPayment").Text)
value += CDec(Item("TotalPayment").Text)
Case Telerik.Web.UI.GridItemType.Footer
'------ Display the total amount in Footer ------
Dim footerItem As GridFooterItem = e.Item
If Not RadGrid1.Items.Count = 0 Then footerItem("TotalPayment").Text = "Total :" + value
End Select
End Sub
Well, if you use custom formula to calculate results instead of standard aggregates like sum, average and so on, you probably have to rely on your own logic. The cells from the group footers should be indexable by unique name - I remember this was not supported with previous Telerik ASP.NET Grid versions but this was added in more recent versions - Q2 or Q3 2009.
Dick

Resources