Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.CellTemplate=cell;
            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);
               designgrid();
               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
Comments
Richard C Bishop 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.
Sandeep Mewara 5-Nov-12 13:57pm    
In Winforms, you dont need to call DataBind().
Richard C Bishop 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.

:thumbsup:
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.

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