| |

VerySource

 Forgot password?
 Register
Search
View: 122|Reply: 10

How to read a row ID in gridview?

[Copy link]

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

Post time: 2020-3-12 21:30:01
| Show all posts |Read mode
I use gridview to display a table, and now I want to update the data in the table, I define the update function myself. .
<asp: BoundField HeaderText = "No" DataField = "DateId" ReadOnly = "True" />
<asp: TemplateField HeaderText = "Date">
   <ItemTemplate>
      <% # Eval ("WDate")%>
   </ ItemTemplate>
   <EditItemTemplate>
       <asp: TextBox ID = "WDate" Text = '<% # Eval ("WDate")%>' runat = "server" Width = "140px" />
   </ EditItemTemplate>
        <ItemStyle Width = "150px" />
</ asp: TemplateField>
Then i updated the place
protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
    {
        int id = (int) GridView1.DataKeys [e.RowIndex] .Values ​​[0];
        string date = ((TextBox) GridView1.Rows [e.RowIndex] .FindControl ("WDate")). Text;
        string wdescription = ((TextBox) GridView1.Rows [e.RowIndex] .FindControl ("WDescription")). Text;
        string status = ((TextBox) GridView1.Rows [e.RowIndex] .FindControl ("Status")). Text;
       
        UpdateRecords (); // This is the update function.
        GridView1.EditIndex = -1;
        BindGrid ();

    }
Say me when running
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
The problem is the first line: int id = (int) GridView1.DataKeys [e.RowIndex] .Values ​​[0];

What should I do? Please enlighten master. .
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-7 10:30:01
| Show all posts
CheckBox cb = (CheckBox)GridView1.Rows[0].FindControl("CheckBox1");
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-7 14:30:01
| Show all posts
If there are multiple lines
for (int i = 0; i <GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cb.Checked)
            {
            }
        }
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

 Author| Post time: 2020-6-9 19:00:01
| Show all posts
Thank you, brother. . .
With the addition of CheckBox, the layout of the page is changed. . My current situation is that after the modification is completed, the update button is clicked, but the result is not updated, and the error mentioned above is reported. The cause of this error is, I think it is a problem when selecting the ID. . So I want to know how to choose this ID.
Reply

Use magic Report

1

Threads

18

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-6-11 08:00:02
| Show all posts
int id = (int)GridView1.DataKeys[e.RowIndex].Value; Try it like this
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-11 17:00:01
| Show all posts
int id = (int)GridView1.DataKeys[i][0].ToString()
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Invalid IP Address

 Author| Post time: 2020-6-13 11:30:01
| Show all posts
Thank you two upstairs. I tested it and it still doesn't work,
The same error after the compilation every day. . .
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-6-15 08:00:01
| Show all posts
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.toString());
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-18 10:00:01
| Show all posts
Bangding!
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-6-19 14:15:02
| Show all posts
Agree with ls
almost
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list