Read data from gridview
-
Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:
<asp:BoundField HeaderText="Price" DataField="SalesPrice" />
I can then read in cell/column data by indexing into the row and indexing to the column I need like this:
String str = (gvItems.Rows[i].Cells[0]).Text;
Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?
-
Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:
<asp:BoundField HeaderText="Price" DataField="SalesPrice" />
I can then read in cell/column data by indexing into the row and indexing to the column I need like this:
String str = (gvItems.Rows[i].Cells[0]).Text;
Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?
-
Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:
<asp:BoundField HeaderText="Price" DataField="SalesPrice" />
I can then read in cell/column data by indexing into the row and indexing to the column I need like this:
String str = (gvItems.Rows[i].Cells[0]).Text;
Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?
-
Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:
<asp:BoundField HeaderText="Price" DataField="SalesPrice" />
I can then read in cell/column data by indexing into the row and indexing to the column I need like this:
String str = (gvItems.Rows[i].Cells[0]).Text;
Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?
If u just want to read the Gridview values then try this : For intGridRowcnt = 1 To GridViewQuery.Rows.Count If intGridRowcnt = GridViewQuery.Rows.Count Then strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(3).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(4).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(5).Text & " " Else strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(3).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(4).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(5).Text & " " strWhereClause = strWhereClause & GridViewQuery.Rows(intGridRowcnt - 1).Cells(6).Text & " " End If Next
-
hi it is always better to use index than using the name. bcoz if the name is changed u have to modify everywhere wherver u use it. for huge projects it is a very tedious job. my suggestion would be that u better use index.
Thanks for your reply. It is more likely for the column order to be changed rather than the name of the data field because the latter is what the gridview control is bound on. The problem is that if the order of the columns changed, and the types of data were different as they are in my grid, the code would break. Whilst each column is a string value, some of those strings are actually numbers and I use a Parse method to parse them. If these methods tried to parse a string which wasn't actually a number, an exception would be raised. Also, I pass the values to a method. If I use indexing, I have to arrange my parameters according to what position I read them in as, like this:
obj = new MyObject
(
(String)(gvItems.Rows[i].Cells[0]).Text, // Product code
(String)(gvItems.Rows[i].Cells[1]).Text, // Description
(String)(gvItems.Rows[i].Cells[6]).Text, // Part Number
Decimal.Parse((gvItems.Rows[i].Cells[2]).Text), // Sales Price
Decimal.Parse((gvItems.Rows[i].Cells[3]).Text), // Quantity
Decimal.Parse((gvItems.Rows[i].Cells[5]).Text), // VAT Rate
Decimal.Parse((gvItems.Rows[i].Cells[4]).Text) // Discount
);It's far better to use the name if at all possible.
-
gauthee wrote:
gvItems.Rows[i]["column name"].Text
This doesn't compile unfortunately :(
-
Thanks for your reply. It is more likely for the column order to be changed rather than the name of the data field because the latter is what the gridview control is bound on. The problem is that if the order of the columns changed, and the types of data were different as they are in my grid, the code would break. Whilst each column is a string value, some of those strings are actually numbers and I use a Parse method to parse them. If these methods tried to parse a string which wasn't actually a number, an exception would be raised. Also, I pass the values to a method. If I use indexing, I have to arrange my parameters according to what position I read them in as, like this:
obj = new MyObject
(
(String)(gvItems.Rows[i].Cells[0]).Text, // Product code
(String)(gvItems.Rows[i].Cells[1]).Text, // Description
(String)(gvItems.Rows[i].Cells[6]).Text, // Part Number
Decimal.Parse((gvItems.Rows[i].Cells[2]).Text), // Sales Price
Decimal.Parse((gvItems.Rows[i].Cells[3]).Text), // Quantity
Decimal.Parse((gvItems.Rows[i].Cells[5]).Text), // VAT Rate
Decimal.Parse((gvItems.Rows[i].Cells[4]).Text) // Discount
);It's far better to use the name if at all possible.
-
No, I don't want to refer to the index of the columns at all. I need to retrieve the data based on the datafield name defined in the .aspx form.
-
Hi, I have a gridview containing several rows of multi-column data. I've bound my grid from an ArrayList specifying bound fields on my gridview columns like this:
<asp:BoundField HeaderText="Price" DataField="SalesPrice" />
I can then read in cell/column data by indexing into the row and indexing to the column I need like this:
String str = (gvItems.Rows[i].Cells[0]).Text;
Knowing exactly which column it is, is error-prone so what I'd like to do is access my data knowing the datafield name, "SalesPrice". Can anyone suggest a good way of doing this? Do I need to have a template?
The best thing i would recommend is as follows: TextBox shorttitle = Bgt_Main.FooterRow.FindControl("txtshorttitle") as TextBox; GridViewRow row= Bgt_Main.SelectedRow/ GridViewRow row = Bgt_Main.Rows[Bgt_Main.SelectedIndex]; TextBox shortt = row.FindControl("short") as TextBox; Label mainid = row.FindControl("lblrow") as Label; and then use: shorttitle.Text/ shorrtt.Text/ mainid.Text so the general syntax would be: = ..... as here "txtshorttitle","short","lblrow" are the ID's of the textbox/label's in the itemtemplate of the girdview/DetailsView. Even if the column name changes like in Bind(..) or Eval(..) you dont have to change the code. I hope this makes everything clear Murthy here