| |

VerySource

 Forgot password?
 Register
Search
View: 694|Reply: 6

How to find data in datatable in button event in datagird or datalist

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Unknown

Post time: 2020-2-2 12:20:01
| Show all posts |Read mode
I first bind the datalist to the database. There are three columns: id, title, and time. Among them, title and time are displayed through two columns in the datalist. However, I do not want to display the id.
If I want to get the title or time column of the row when I click the button in the datalist, it is very simple. I do this
private void datalist_ItemCommand (object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if (e.CommandName == "b")
{
lb.Text = ((System.Web.UI.WebControls.Label) e.Item.FindControl ("lb1")). Text;
          }
}
Then I want to get the id of the row where I write it. I have bound the id column to the datalist, but there is no control key in the datalist to display this id. The main thing is that I cannot use the datakey because this has already been given Listed. Is there a general method, as long as the datalist or datagrid is bound, then you can get the value of any item in any row of datatalbe
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-3-19 23:15:01
| Show all posts
DataRowView row = (DataRowView) e.Item.DataItem;
Just take row ["field"].
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-25 13:00:01
| Show all posts
Object reference is not set to an instance of the object.
Reply

Use magic Report

0

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-30 01:30:01
| Show all posts
int a = Convert.toint32 (DataGrid1.dataKeys [e.item.itemindex] .tostring ());
At the time of binding, DataGrid1.DataKeyFiled = "id"; then you can get the ID of the current row through the above code
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-5 22:30:01
| Show all posts
I know this method, thank you, but the problem is
DataRowView dr = (DataRowView) e.Item.DataItem;
lb.Text = dr ["hid"]. ToString ();
I can't bind this method
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-1 16:45:01
| Show all posts
Bind the CommandArgument of the button to the ID column CommandArgument=<% DataBinder(....)%>

You can get it directly. I did this before
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-21 20:00:01
| Show all posts
Use datalist
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