简体   繁体   中英

Gridview changing text in a button column after data bind

I have a GridView in a ASP.NET web application, in which I have added buttons in each row:

<asp:GridView ID="gridviewdatadosen" runat="server" AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-hover" OnRowDataBound="gridviewdatadosen_RowDataBound" OnSelectedIndexChanged="gridviewdatadosen_SelectedIndexChanged" OnRowCommand="gridviewdatadosen_RowCommand">
    <Columns>
        <asp:BoundField DataField="NIK" HeaderText="NIK" SortExpression="NIK"></asp:BoundField>
        <asp:BoundField DataField="NIDN" HeaderText="NIDN" SortExpression="NIDN"></asp:BoundField>
        <asp:BoundField DataField="NAMA" HeaderText="NAMA" SortExpression="NAMA"></asp:BoundField>
        <asp:BoundField DataField="Alamat" HeaderText="Alamat" SortExpression="Alamat"></asp:BoundField>
        <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
                <asp:Button ID="btnstatus" runat="server" Text="Aktif" CssClass="btn btn-primary" CommandName="aktifasi"  CommandArgument='<%# Eval("NIK") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I fill the datagridview on the server side. I filled it with taking the data in the database.

                DataTable dt = new DataTable();
                DataTable dt1 = new DataTable();
                SqlConnection conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=SKRIPSI;User ID=sa;Password=sa");
                conn.Open();
                string ngisi = "SELECT [nik] as 'NIK' , [nidn] as 'NIDN', [nama] as 'NAMA', [alamat] as 'Alamat' FROM [dosen]";
                SqlCommand comm = new SqlCommand(ngisi, conn);
                dt.Load(comm.ExecuteReader());
                conn.Close();
                gridviewdatadosen.DataSource = dt;
                gridviewdatadosen.DataBind();
                int tmp = dt.Rows.Count;

after I fill the datagridview, I wanted to check the status of dosen whether he is active or not by select id and status of dosen.

                conn.Open();
                string check = "SELECT nik, status FROM [dosen]";
                comm = new SqlCommand(check, conn);
                dt1.Load(comm.ExecuteReader());
                conn.Close();

I have tried to change the existing text on the button but did not succeed.

                        for (int i = 0; i < tmp; i++)
                    {
                        if (dt1.Rows[i][1].ToString() == "Aktif")//check the dosen aktif or not
                        {
                            for (int j = 0; j < tmp; j++)
                            {
                                if (dt.Rows[j][0].ToString() == dt1.Rows[i][0].ToString())// check nik where status = 'Aktif'
                                {
                                    // I want to change the button in each row. if he 'Aktif' then the text in button will change to be 'aktif'
                                    //do not know what to do
                                }
                            }
                        }
                    }

I want to change the button in each row. if he 'Aktif' then the text in button will change to be 'aktif'. help me to solve this problem. Sorry if my english or my explain is bad. Thank You

You can loop all the rows, find the Button and change the Text based on the values in dt1

foreach (GridViewRow row in GridView1.Rows)
{
    //find the button in the row with findcontrol and cast back to a button
    Button button = row.FindControl("btnstatus") as Button;

    //check dt1 and set button text
    button.Text = "Active";
}

Or you can do the same on the GridView databinding with the OnRowDataBound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //find the button in the row with findcontrol and cast back to a button
        Button button = e.Row.FindControl("btnstatus") as Button;

        //check dt1 and set button text
        button.Text = "Active";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM