Searcher
Searcher

Reputation: 1855

How to bind variables data into GridView in windows forms

Hello all I have some variable in my program in for loop and the values for variables will come dynamically in the for loop. I want to bind those values to grid view. I know how to bind the SQL server data to grid. But how to handle here. How to arrange the column values. Can any one help me please.

I have these variables

string changedFile  
int parentIssue 
List<String> Authors

I want to add these 3 fields into grid view in for loop. Is there any way to write out side of the for loop?

Upvotes: 0

Views: 3531

Answers (3)

nunespascal
nunespascal

Reputation: 17724

Just assign the list as a datasource.

dataGridView1.DataSource = Rows;

You can arrange columns in the select statement.

private System.Windows.Forms.DataGridViewComboBoxColumn Authors;
this.Authors.HeaderText = "Authors";
this.Authors.Name = "Authors";
this.dataGridView1.Columns.Add(this.Authors);
this.Authors.Items.AddRange(new object[] {
            "Ayn Rand",
            "Tagore"});

Upvotes: 1

Likurg
Likurg

Reputation: 2760

You can try something like this

public class BindingObject
{
    public int intMember;
    public string stringMember;
    public string nullMember;
    public BindingObject(int i, string str1, string str2)
    {
        intMember = i;
        stringMember = str1;
        nullMember = str2;
    }
} 

////Somewhere
ArrayList list = new ArrayList();
list.Add(new BindingObject(1, "One", null));
list.Add(new BindingObject(2, "Two", null));
list.Add(new BindingObject(3, "Three", null));
dataGrid1.DataSource = list;

Upvotes: 1

Krishnanunni Jeevan
Krishnanunni Jeevan

Reputation: 1759

if you have to set in the loop itself you have to define a template for a gridview in aspx

<asp:GridView ID="gridViewField" ClientIDMode="Static"   runat="Server" AutoGenerateColumns="false"
               onrowdatabound="gridViewField_RowDataBound"  HorizontalAlign="Center"  AlternatingRowStyle-BackColor="#ddecfe" >
               <Columns>                   
                   <asp:TemplateField HeaderText="Field Name">
                       <ItemTemplate>
                           <asp:Label runat="server" ID="lblFieldName"></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
</columns>
</asp:GridView>

In the for loop you can get each row in codebehind cs file as follows

var lblFieldName= gridViewField.Rows[i].FindControl("lblFieldName") as Label
lblFieldName.Text=your loop data

i is your loop variable

Upvotes: 0

Related Questions