Hi kevinf,
You need to use GridView OnRowCreated event.
To hide the Id column set the Cell Visible property to false.
To set the Width and BackColor set the Width and BackColor to respective value.
Refer below example.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="gvEmployees" runat="server" Style="z-index: 1; left: 10px; top: 0px; position: absolute; height: 133px; width: 426px"
ShowHeaderWhenEmpty="True" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" OnRowCreated="OnRowCreated">
<AlternatingRowStyle BackColor="#CCCCCC" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" Width="100%" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 1 EmployeeId,FirstName,Notes FROM Employees";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
}
}
}
}
}
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
// Hide the Id Column.
e.Row.Cells[1].Visible = false;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Set the width and Back Color of 2nd Column.
e.Row.Cells[2].Width = 50;
e.Row.Cells[2].BackColor = Color.Aqua;
// Set the width and Back Color of 3rd Column.
e.Row.Cells[3].Width = 300;
e.Row.Cells[3].BackColor = Color.Brown;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT TOP 1 EmployeeId,FirstName,Notes FROM Employees"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
End Using
End Using
End Using
End If
End Sub
Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
' Hide the Id Column.
e.Row.Cells(1).Visible = False
If e.Row.RowType = DataControlRowType.DataRow Then
' Set the width and Back Color of 2nd Column.
e.Row.Cells(2).Width = 50
e.Row.Cells(2).BackColor = Color.Aqua
' Set the width and Back Color of 3rd Column.
e.Row.Cells(3).Width = 300
e.Row.Cells(3).BackColor = Color.Brown
End If
End Sub
Screenshot