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