Change ASP.Net GridView Cell value based on another cell in RowDataBound event using C# and VB.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

i have Data Textbox in gridview and and textbox is hours

like

 Date Hours
2018-05-26 21:22:00 1
  0

Now i want when Date does not exit then hours will be equal to 0.

I tried this but it is not working 

if (txtINTIME.Text == 0)
{
    txtHours = "0";
}
                      

Kindly guide ..Thanks

You are viewing reply posted by: pandeyism 6 months ago.
Posted 6 months ago

Hey akhter,

Please refer below sample.

HTML

<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvCustomers_OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Date" HeaderText="Date" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtHoures" runat="server" Text='<%#Eval("Hours") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name", typeof(string)), new DataColumn("Date", typeof(string)), new DataColumn("Hours", typeof(string)) });
        dt.Rows.Add("Mudassar", "2018-05-26 21:22:00", "1");
        dt.Rows.Add("John", "2018-05-27 21:22:00", "1");
        dt.Rows.Add("test");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
}
protected void gvCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string date = e.Row.Cells[1].Text;
        if (date == "&nbsp;")
        {
            TextBox txtHours = e.Row.FindControl("txtHoures") as TextBox;
            txtHours.Text = "0";
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("Name", GetType(String)), New DataColumn("Date", GetType(String)), New DataColumn("Hours", GetType(String))})
        dt.Rows.Add("Mudassar", "2018-05-26 21:22:00", "1")
        dt.Rows.Add("John", "2018-05-27 21:22:00", "1")
        dt.Rows.Add("test")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End If
End Sub

Protected Sub gvCustomers_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim date1 As String = e.Row.Cells(1).Text

        If date1 = "&nbsp;" Then
            Dim txtHours As TextBox = TryCast(e.Row.FindControl("txtHoures"), TextBox)
            txtHours.Text = "0"
        End If
    End If
End Sub

Screenshot