Add extra column to grid using c#

sweet55
sweet55 used Ask the Experts™
on
Add an extra column to the grid, this column will be the count, Populate each row starting with 1 up to whatever is  the count using c#. It should be less than 5 lines of code.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try using this...
it worked for me...
 
<asp:TemplateField>
  <ItemTemplate>
    <%# Container.DataItemIndex + 1 %>
  </ItemTemplate>
</asp:TemplateField>

Hope this will fix up your issues.

Author

Commented:
Thnaks for your answer but i was expecting the code to be in backend using c#.
You can display the row numbers like shown below:

//To add First column as rownumber
DataGridViewTextBoxCell newCell = new DataGridViewTextBoxCell();
DataGridViewColumn SNOCol = new DataGridViewColumn(newCell);
SNOCol.DisplayIndex = 0;
SNOCol.HeaderText = "SNO";
SNOCol.Name = "SNO";
DataGridView1.Columns.Add(SNOCol);

int cnt = DataGridView1.Rows.Count;
int i;
for (i = 1; i <= cnt; i++)
{
DataGridView1.Rows[i - 1].Cells["SNO"].Value = i;
}

Open in new window

Now during sorting or filtering, just remove the cell from datagridview and again add it.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

OR you can try another way like below

1. Add this your GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated1" >

  <Columns>
            <asp:TemplateField HeaderText="myRowid">
            <ItemTemplate><asp:Label runat="server" />                      
            </ItemTemplate>            
            </asp:TemplateField>

...

</Columns>

Open in new window


2. Add this to your code page
protected void GridView1_RowCreated1(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{

	if (e.Row.RowType == DataControlRowType.DataRow) {
		// Retrieve the Label from the first column.
		Label myLabel = (Label)e.Row.Cells[0].Controls[0];

		// Set Label text equal to the rowIndex +1 
		myLabel.Text = e.Row.RowIndex.ToString() + 1;

	}
}

Open in new window


Hope above might help you.

Author

Commented:
Thanks for ur answer. I tried the solution above which didnt work.

This worked for me..
int i = 1;
        protected void Gridviewe1_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                Label lblRow = (Label)e.Row.FindControl("lblRowNumber");
                lblRowNumber.Text = i.ToString();
                i++;
            }
        }
Ok, that's grt.
But, i would reccommend you to use RowCreated event instead of RowDataBound Event.

Author

Commented:
Thanks for your suggestions. I tried ur code using RowCreated event but it generated numbers like 01, 11, 21,31etc..
Sorry,the line below was  incorrect
myLabel.Text = e.Row.RowIndex.ToString() + 1;

Open in new window


it is appending the string like "1" + 1 = "11". You need to change the code to

myLabel.Text = (e.Row.RowIndex + 1).ToString();

Open in new window

you can also use your own login (about i++) in the rowCreated Event like below

int i =1;
protected void GridView1_RowCreated1(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{

	if (e.Row.RowType == DataControlRowType.DataRow) {
		// Retrieve the Label from the first column.
		Label myLabel = (Label)e.Row.Cells[0].Controls[0];

		myLabel.Text = i.ToString();
i++;

	}
}

Open in new window

Author

Commented:
Thanks for Your Solutions.
If my solution assist you in any ways then  award points and Please close this post.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial