Click here to Skip to main content
12,952,398 members (40,577 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi friends

The below is my code for binding data to gridview.
This method for creating datagridview column

public void designgrid()
            DataGridViewColumn firstcolumn=dataGridView1.Columns[1]; 
            DataGridViewCell cell=new DataGridViewTextBoxCell();
            firstcolumn.HeaderText = "AdmissionNumber";
            firstcolumn.Width = 50;
            DataGridViewColumn secondcolumn = dataGridView1.Columns[2];
            DataGridViewCell cell1 = new DataGridViewTextBoxCell();
            secondcolumn.CellTemplate = cell1;
            secondcolumn.HeaderText = "StudentName";
            secondcolumn.Width = 80;
            DataGridViewColumn Thirdcolumn = dataGridView1.Columns[3];
            DataGridViewCell cell2 = new DataGridViewTextBoxCell();
            Thirdcolumn.CellTemplate = cell1;
            Thirdcolumn.HeaderText = "StanderedName";
            Thirdcolumn.Width = 80;
            DataGridViewColumn forthcolumn = dataGridView1.Columns[4];
            DataGridViewCell cell3 = new DataGridViewTextBoxCell();
            forthcolumn.CellTemplate = cell3;
            forthcolumn.HeaderText = "RegisterNumber";
            forthcolumn.Width = 50;
            DataGridViewColumn fifthcolumn = dataGridView1.Columns[5];
            fifthcolumn = new DataGridViewComboBoxColumn();
            fifthcolumn.HeaderText = "SortNo";
            fifthcolumn.Width = 50;


This code for binding data to gridview
DataTable studentbind = ibl4.studentbindforfemale(sdt);
               int rowcount = studentbind.Rows.Count;
               List<string> no = ibl4.setnoofint(rowcount);
               int registerno = ibl4.getregiter(sdt);
               for(int i=0;i<rowcount;i++)
                   dataGridView1.Rows[i].Cells[0].Value = studentbind.Rows[i]["AdmissionNumber"].ToString();
                   dataGridView1.Rows[i].Cells[1].Value = studentbind.Rows[i]["StudentName"].ToString();
                   dataGridView1.Rows[i].Cells[2].Value = studentbind.Rows[i]["Standered_name"].ToString();
                   dataGridView1.Rows[i].Cells[3].Value = registerno;


Here "Datatable studentbind" contains first 3 columns value as specified in the name in code.
fourth column is textbox column which acutally add as a extra column that want to contain the value of "Registerno" for all row.
fithcolumn i have created as a combobox column. but i dont know how can bind data to this column. the bind data is "List<string> no";
and also please help how can add data to gridviewtextboxcolumn.
still i didnt run my code. so i dont know whether is in the code. if error come please help me to solve the problem
Posted 5-Nov-12 6:16am
richcb 5-Nov-12 12:21pm
I don't see where you called the dataGridView1.DataBind(). Nothing will bind if you don't call that method.
baskaran chellasamy 5-Nov-12 13:42pm
This is the windows application. so we cannot use databind.but we can use datasource method. but i want manually bind data to girdview,because i from datatable i just bind three column and add two column from other nature. inside the forloop we can bind datasource.If my openion is wrong,then tell me how can i do this.
Sandeep Mewara 5-Nov-12 13:57pm
In Winforms, you dont need to call DataBind().
richcb 5-Nov-12 14:03pm
My mistake, I saw nowhere that specified it was Winforms and I am not very familiar with it to pick out by just seeing the code.
Sandeep Mewara 5-Nov-12 14:16pm
That's okay. Just shared the info with you.

About winforms, generally the tag of just C# or VB.NET is for winforms, though not always. It's best for enquirers to clearly state in their question to avoid assumptions.

Gnanavadivel 1-Mar-13 6:10am

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,289
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,900

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100