Hi simflex,
Check the below example.
HTML
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false" OnRowDataBound="Gridview1_RowDataBound">
<Columns>
<asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Name">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:TextBox ID="txtsourcename" Text='<%# Eval("sourcename") %>' placeholder="Name...(e.g, Jane Doe)" runat="server" Style="width: 375px;" AutoPostBack="true" class="form-control textClass"></asp:TextBox><br />
<asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" /><span style="color: #ff0000">*Check this box if N/A</span>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="txtsourceaddress" Text='<%# Eval("sourceaddress") %>' placeholder="Address..." runat="server" Style="width: 375px;" class="form-control textClass"></asp:TextBox><br />
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add another row if needed" CssClass="grvAddButton" /><br />
<br />
<br>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="sourceDelete" runat="server" Text="Delete" CommandName="Delete"
CssClass="grvDelButton" OnClientClick="return confirm('Are you sure you want to remove this row?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaes
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
Imports System.Data
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("RowNumber"),
new DataColumn("sourcename"),
new DataColumn("sourceaddress") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "", "");
dt.Rows.Add(4, "", "");
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex > 0)
{
string sourceName = (e.Row.FindControl("txtsourcename") as TextBox).Text;
string txtSourceAddress = (e.Row.FindControl("txtsourceaddress") as TextBox).Text;
if (string.IsNullOrEmpty(sourceName) && string.IsNullOrEmpty(txtSourceAddress))
{
e.Row.Visible = false;
}
else
{
e.Row.Visible = true;
}
}
}
}
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("RowNumber"),
New DataColumn("sourcename"),
New DataColumn("sourceaddress")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "", "")
dt.Rows.Add(4, "", "")
Gridview1.DataSource = dt
Gridview1.DataBind()
End If
End Sub
Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.RowIndex > 0 Then
Dim sourceName As String = (TryCast(e.Row.FindControl("txtsourcename"), TextBox)).Text
Dim txtSourceAddress As String = (TryCast(e.Row.FindControl("txtsourceaddress"), TextBox)).Text
If String.IsNullOrEmpty(sourceName) AndAlso String.IsNullOrEmpty(txtSourceAddress) Then
e.Row.Visible = False
Else
e.Row.Visible = True
End If
End If
End If
End Sub