HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="id"
OnRowDataBound="GridView1_OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="Gate1">
<ItemTemplate>
<asp:Label ID="lblGate1" runat="server" Text='<%# Eval("Gate1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gate2" >
<ItemTemplate>
<asp:Label ID="lblGate2" runat="server" Text='<%# Eval("Gate2") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gate3">
<ItemTemplate>
<asp:Label ID="lblGate3" runat="server" Text='<%# Eval("Gate3") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gate4">
<ItemTemplate>
<asp:Label ID="lblGate4" runat="server" Text='<%# Eval("Gate4") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateGate();
}
}
private void PopulateGate()
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
string sqlStatment = "SELECT * from Gate";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
}
}
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int id = Convert.ToInt32(this.GridView1.DataKeys[e.Row.RowIndex].Value);
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
string sqlStatment = "SELECT Bays from Bays Where Id = @Id";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@Id", id);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
int bays = Convert.ToInt32(ds.Tables[0].Rows[0]["Bays"].ToString());
if (bays == 2)
{
(e.Row.Cells[2].FindControl("lblGate3") as Label).Text = "NA";
(e.Row.Cells[3].FindControl("lblGate4") as Label).Text = "NA";
}
if (bays == 3)
{
(e.Row.Cells[1].FindControl("lblGate2") as Label).Text = "NA";
(e.Row.Cells[2].FindControl("lblGate3") as Label).Text = "NA";
(e.Row.Cells[3].FindControl("lblGate4") as Label).Text = "NA";
}
// likewise generate the code for others
}
}
}
}
}
}
Image:

As you can see row 1 and 2 are changed. i am comparing the DataKeyValue which is Id to the Bays table column Bays