473,486 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Get the rowindex of gridview outside the gridview

58 New Member
Hi,

I created a gridview and outside the gridview i created a button.In the grid
i am updating one value in the textbox. After entering the values when clicking
on the button outside the grid it need to get the row index and calculate the total based on the entered value in textbox. How i can get the row index outside the gridview.


Pls help me

Urgent....

Thanks in advance...


Rani
Nov 3 '09 #1
1 11393
liawcv
33 New Member
What is the purpose of getting the RowIndex? To perform a database update to that particular row? Or to calculate the vertical / horizontal total? Is the TextBox shown on editing mode or normal mode? These information may help others to understand your problem clearer.

1. Simply to get the vertical total

RowIndex is not essential if the purpose is to get the vertical total. You can recalculate the total every time the button is clicked. The process should not be expensive (I guess so). The following codes may not suit to your case, but should be good enough to illustrate the idea:

Expand|Select|Wrap|Line Numbers
  1. TextBox txt;
  2. int total = 0;
  3. foreach (GridViewRow row in gv.Rows)
  4. {
  5.    txt = row.FindControl("txtSomething") as TextBox;
  6.    total += int.Parse(txt.Text);
  7. }
  8. lblTotal.Text = total.ToString();
2. Simply to get the horizontal total

Similar concept as (1). Just loop through the GridViewRowCollection and calculate the horizontal total for each row. Do it every time the button is clicked. RowIndex is not essential.

3. Get the RowIndex

If it is still necessary to get the RowIndex, here is my solution: Let me assume that you would like to know the RowIndex of the row(s) on which the TextBox(es)' value has been changed. The TextBox(es) appear on normal mode and is placed within a TemplateField.

Firstly, create a global List of integers. This is used to keep track of the RowIndex. I assume that you would like to keep track of multiple RowIndex as the user may change more than one TextBox value at a time.

Expand|Select|Wrap|Line Numbers
  1. List<int> list = new List<int>();
Then, create the TextChanged event handler for the TextBox. The TextBox's AutoPostBack property should be retained as "False". I assume that you do not want to perform any action before the button is clicked.

Expand|Select|Wrap|Line Numbers
  1. private void txtSomething_TextChanged(object sender, EventArgs e)
  2. {
  3.    TextBox txt = sender as TextBox;
  4.    GridViewRow row = txt.Parent.Parent as GridViewRow;
  5.    list.Add(row.RowIndex);
  6. }
txt.Parent --> The cell containing the TextBox.
txt.Parent.Parent --> The row containing the cell which containing the TextBox.

Finally, at the button's Click event handler, access the RowIndex from the global List of integers. The list should already been filled with RowIndex since, in this case, TextChanged event handler will be executed before Click event handler.

Expand|Select|Wrap|Line Numbers
  1. private void btnSomething_Click(object sender, EventArgs e)
  2. {
  3.    foreach (int i in list)
  4.    {
  5.       // The RowIndex can be accessed through variable i.
  6.    }
  7. }
After all, this may not suit to your case. However, it is good to share ideas... : )
Nov 3 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1015
by: Jesse Johnson | last post by:
I am trying to have one gridview control that displays items with the select command enabled and when the user clicks "select" it would send that line of information to another gidview control....
0
1082
by: David | last post by:
How would I go about having a GridView in another GridView and have it display only data for the selected Row? I have a GridView of contact names, title, etc. at a company and would like to have a...
1
1829
by: mbharathiraja | last post by:
I am doing a shopping cart in asp.net I am using a gridview.when a user checks the rows of grid view it should get selected. now, through a button(OUTSIDE Gridview),I should be able to...
2
1214
by: mharness | last post by:
Hello, Can anyone tell me how to bind a dataset to a gridview in a gridview row during the rowediting event? Thanks, Mike
0
1972
by: David C | last post by:
I have a GridView that displays a list of contact names, email address, etc. Inside of it, I have another GridView that shows the 1 or more phone numbers for that contact and I set it to...
1
1612
by: Cirene | last post by:
This is confusing... I have a gridview (gv1) with another gridview (gv2) inside of it. gv1 has a bunch of house listings and within each row is a group of related house pictures (gv2). I'm...
1
2170
by: Vyas111111 | last post by:
Hello All, How can i get the value of a gridview cell outside any GridView event
0
1318
by: sebastianthegreatful | last post by:
I have a program and a list of exercises. The program is actually also just a list of exercises. On the screen I try to show my database content in two gridviews. with two linqdatasources...
12
7253
by: dorandoran | last post by:
I followed this link to add new record from gridview. So far it's good but I need to modify the last piece to edit the record that I will supply the record id. ...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.