Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Good Afternoon, My friend,

Now ,I am developing a project in C# WinForm & i need a DataGridView control that displays Serial Number on the left side and Statistics Column's value and display it under the column and on the bottom row.

Up to today, I can't make it out ,Can you Help me?
Thank you very much!
(MyDataGridView.DataResource=MyDataSet.Table["MyTable"];
Posted
Updated 28-Dec-13 23:16pm
v4
Comments
Sandeep Mewara 7-Jan-11 5:32am    
Your question is not quite clear.
nagendrathecoder 7-Jan-11 5:51am    
It is difficult to understand your question, can you please explain it a bit more.
Ankur\m/ 7-Jan-11 6:04am    
I understand that English is not your first language. But your question doesn't tell much about your problem. So I suggest you to phrase the question in you language and use Google Translate to convert it into English. That may help. Or we also have a Chinese forum (your profile says you are from China) to help you. Here: http://www.codeproject.com/Forums/1580230/Chinese.aspx
agent_kruger 29-Dec-13 5:16am    
please refrase your question or tell us what you want to achieve.

1) Bind the DataGridView control to some BindingSource and setup its Columns
2) Add another column (Unbound Column) and make it the first column
3) Set its name = ColumnSNo
4) Set its ReadOnly = True
4) Set the DataGridView control's VirtualMode property to True
5) In CellValueNeeded event use the following code:


VB
Private Sub DataGridView1_CellValueNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValueEventArgs) Handles DataGridView1.CellValueNeeded

    If e.RowIndex >= 0 AndAlso e.ColumnIndex = 0 Then
        e.Value = e.RowIndex + 1
    End If

End Sub
 
Share this answer
 
v2
Comments
nitin503 11-Sep-12 3:26am    
serial number assign nicely for each new generated rows but the data in another column disappear after clicking on the another cell or row of grid view. So how to maintain the data in another cell after giving the serial number to each row. thnks in advance.nitin
XML
<asp:TemplateField HeaderText="Sr.">
                                   <ItemTemplate>
                                    <%#Container.DataItemIndex+1 %>
                                   <</ItemTemplate>
                               </asp:TemplateField>
 
Share this answer
 
For "Row-Header Serial Number on the left side"

See below link.

DataGridView - Display Row Number(Left side)[^]

For "Statistics" ( I think you want like This[^])

Go through that link. Might be helpful.
 
Share this answer
 
Comments
LGA1234 7-Jan-11 8:36am    
I can't open this first linked web page.
TweakBird 8-Jan-11 0:48am    
It's opening for me. Is that blocked in your organization.?
Hi,

I don't know it this are you are looking for, but I hope that help you.

You may show the Row Header Serial Number programatically.

You can make a loop and asign every serial number to the row header:

C#
foreach (DataGridViewRow row in dataGridView.Rows)
{
    row.HeaderCell.Value = "serial number";
}


After that, you can hide the serial number column if it's in the DataTable.

But you can't show a statistics row at the end if you don't put into DataTable.
To solve it, you can calculate it after fill the dataTable and add the row to it. Do the same with the statistics column.
 
Share this answer
 
Comments
LGA1234 7-Jan-11 8:38am    
You can make a loop and asign every serial number to the row header?

How to do it? May you show me the codes? Thanks!
I supose that the serial number is in a column named "SC" of the DataTable:

C#
foreach (DataGridViewRow row in dataGridView.Rows){

    row.HeaderCell.Value = row.Cells["SC"].Value.ToString;

}
 
Share this answer
 
v2
C#
//1-add new column to  dataGridView1 call it ser or your Name
//2-set it's DataPropertyName To None "Very Important"
//3-Set HeaderText to ("Your Name To shown In Header")
//4-Set frozen To True
//5-in DefultCellStyle Do Thise
//  A-Set BackColor To AnyColor you Want
//  b-Set Null Value to 0 or 1 as you prefer
//  b-Set Alignment To middleCenter
//6- call Method MakeSerieal() everyTime You Want To fill dataGridView1
//7- call Method MakeSerieal() In ColumnHeaderMouseClick ("It help if you Want To Reorder dataGridView1")

//--------------------------
private void MakeSerieal()
{

  if (dataGridView1.Rows.Count > 1)
                        {
                            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                            {
                                dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
                            }
                        }
}
//--------------------------

//With My Regards
//Ahmed Mansour ("Egypt")
//Mansour_VbKing@Yahoo>com
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900