簡體   English   中英

如何將變量數據綁定到Windows窗體中的GridView

[英]How to bind variables data into GridView in windows forms

大家好,我在for循環中的程序中有一些變量,變量的值將在for循環中動態生成。 我想將這些值綁定到網格視圖。 我知道如何將SQL服務器數據綁定到網格。 但是如何處理這里。 如何排列列值。 任何人都可以幫助我。

我有這些變量

string changedFile  
int parentIssue 
List<String> Authors

我想在for循環中將這3個字段添加到網格視圖中。 有沒有辦法寫出for循環的一面?

只需將列表指定為數據源即可。

dataGridView1.DataSource = Rows;

您可以在select語句中排列列。

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"});

你可以嘗試這樣的事情

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;

如果你必須在循環中設置,你必須在aspx中為gridview定義一個模板

<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>

在for循環中,您可以獲取codebehind cs文件中的每一行,如下所示

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

我是你的循環變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM