473,609 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
+ 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 11431
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 GridViewRowColl ection 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.Pare nt --> 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
1019
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. That way the second gridview only has the data that the user selcted. Could anyone help me out? Thanks in advance. Jesse Johnson
0
1087
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 Gridview of additional phone numbers show inside in a <tdwhen selected, but only that contacts additional phones. Can anyone point me to where I can find out how this can be accomplished? Thank you. David
1
1841
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 insert the selected rows into database
2
1219
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
1978
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 Visible=False. When I select the line (using a LinkButton) I am making the embedded GridView (phone numbers) Visible on that SelectedIndex. However, when I select a different contact in the GridView the previous contact line/row still shows their phones...
1
1622
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 having trouble getting gv2 to show any data. Here's part of my code...
1
2185
by: Vyas111111 | last post by:
Hello All, How can i get the value of a gridview cell outside any GridView event
0
1322
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 associated. Eventually I want to be able to drag form the exercise list gridview into the program grid view but for now a button will do. The question goes, is the gridview control the right control to use or does anyone here have a better suggestion?
12
7265
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. http://asptutorials.net/ASP/gridview-auto-edit-blank-row/ I am using stored proc to insert a new record and returning the id. Now I can get the id in my asp.net (c$) environment. I added a label and I was able to populated the label with the newly added record's
0
8129
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8535
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8404
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5509
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4017
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4080
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2530
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 we have to send another system
1
1667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1386
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.