| |

VerySource

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

Help change a piece of code, thank you

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-14 10:30:01
| Show all posts |Read mode
I added a System.Web.UI.WebControls.Button to the records of each day in the DataGrid. After binding the data set, when I press the button in the record, only the current record is displayed. I write as follows Yes, but it can't be realized, how can I change it, thank you!

//Code when the page loads
protected void Page_Load(object sender, EventArgs e)
    {
        string FSQLStr = "select * from T_NoteBook ";
        System.Data.SqlClient.SqlDataAdapter FsqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter(FSQLStr, Fconnection);
        FsqlDataAdapter.Fill(FDataSet,"T_NoteBook");
        DataGrid1.DataSource = FDataSet.Tables["T_NoteBook"].DefaultView;
        DataGrid1.DataKeyField = "FID";
        DataGrid1.DataBind();
    }

//Error code
protected void del_Click(object sender, EventArgs e)
    {
        FDataSet.Reset();
        //The main reason is that the following is wrong, but I don't know how to get the DataKeys value of the current record.
        System.Web.UI.WebControls.DataGridCommandEventArgs E1 = (System.Web.UI.WebControls.DataGridCommandEventArgs)e;
        string FID = (string)(DataGrid1.DataKeys[E1.Item.ItemIndex]);
        string sqlstr = "select * from T_NoteBook where FID =" + FID;
        System.Data.SqlClient.SqlDataAdapter FsqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlstr, Fconnection);
        FsqlDataAdapter.Fill(FDataSet, "T_NoteBook");
        DataGrid1.DataSource = FDataSet.Tables["T_NoteBook"].DefaultView;
        DataGrid1.DataKeyField = "FID";
        DataGrid1.DataBind();
    }
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-14 12:45:01
| Show all posts
You set a name in the button CommandName,
Then use this ItemCommand property to get the row corresponding to which button you pressed,
Don't forget to use the DataKeyField property when binding
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-14 13:00:01
| Show all posts
if(e.CommandName=="The name you set above")
{
//The corresponding operation
}
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-10-14 13:30:01
| Show all posts
Use SelectedCommadn binding!
Reply

Use magic Report

0

Threads

15

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-10-14 14:00:01
| Show all posts
Get datakeys?
DataKey DataKey = DataGrid1.DataKeys[i]
i is the number of rows
See if you can use the datagrid so used in gridView. I still dare not say that I have never used it. Ashamed
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-10-14 14:30:01
| Show all posts
SelectedCommand
Reply

Use magic Report

0

Threads

24

Posts

20.00

Credits

Newbie

Rank: 1

Credits
20.00

 China

Post time: 2020-10-14 14:45:01
| Show all posts
Set the CommandArgument of the button as the KEY of the record
after that:

if(e.CommandName=="The name you set above")
{
  int Id=Convert.ToInt32(e.CommandArgument); //Get the KEY value
//The corresponding operation
}
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