我可以将列表作为源传递给gridview吗??? [英] Can I pass list to gridview as a source???

查看:69
本文介绍了我可以将列表作为源传递给gridview吗???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我的任务是从Outlook导入联系人.
用户要以.csv格式导出文件,我必须解析该数据并从该文件中获取名字,姓氏和电子邮件.

这是其中的一部分..我能够分成每一行,然后尝试创建一个数组.

如果我从该数组中收集想要的字段并将其存储在对象中
像Myobject.FistName ="balaa"
Myobject.LastName ="jddk"
.Email ="dad@hakdj.com"

1)如果我创建一个列表,则可以从数组中传递那些对象,该List< myobjects>以gridview作为数据源.
2)在底部代码中从字符串数组进行解析时,我还有一个问题...数组中的第一个元素是文件中的菜单而不是值...

我在底部代码中所说的是"fieldOnLine [0]"是MENU名称(例如FirstName,LastName.)不是用户详细信息,所以我必须避免使用它.是否必须使用for循环来避免这些值? ???


Hi guys,
My task is to import contacts from Outlook.
user going to export the file in .csv format I have to parse that data and take firstname, lastname and Email from that file.

Part of that is this.. I am able to separate into each lines then tring to get create an array.

If I collect fields I want from that array and store it in an object
like Myobject.FistName="balaa"
Myobject.LastName = "jddk"
.Email ="dad@hakdj.com"

1) If I create a list those objects from the array can I pass that List<myobjects> to gridview as data source.. ???

2) While parsing from string array in the bottom code I have one more problem... the first element in the array was menu from the file not the values...

what I am saying is "fieldOnLine[0]" in the bottom code is MENU names(like FirstName, LastName.. Not the user details so I have to avoiid it. DO I have to use for loop instead, to avoid those values????


using (Stream s = file.InputStream)
{
  string[] numberOfUsers;
  var sr = new StreamReader(s);
  string contacts = sr.ReadToEnd();
  contacts = contacts.Replace("\r\n", "\n");
  string[] separateLines = contacts.ToString().Split(''\n'');
  foreach (string line in separateLines)
{
  numberOfUsers = line.Split('','');
  // now you can go through the field
  foreach( string fieldOnLine in numberOfUsers )
  {
    // process fieldOnLine here
  }
}






我知道我在问一些愚蠢的问题.

感谢您的帮助....






I know I am asking silly questions..

Thanks for your help ....

推荐答案

是的,您是对的,您可以肯定地将对象列表作为数据源提供,因为gridview可以处理任何实现iEnumerable接口.
假设您有一个名为classA的类.然后提供类型为classA的对象列表.您可以执行以下操作:
Yes dude , you are right , you can surely provide list of objects as the datasource , as gridview can handle any source that implements iEnumerable interface.
suppose you have a class named classA. then to provide list of objects of type classA. you can do something as below:
List<classA> lst=new List<classA>();

// code to add items to list
GridView1.Datasource=lst;
GridView1.DataBind();

// If the Class classA has properties named Prop1 and Prop2 ,you should create columns(that can be boundfield , templatefield,etc.)and set the appropriate Property name in the data field.example


< GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                    <Columns>
                        <asp:BoundField DataField="Prop1" HeaderText="Property 1" />
                        <asp:TemplateField HeaderText="Property 2">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Prop2") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Prop2") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>


是的,您可以使用for循环来解决第二个问题.
您也可以使用相同的foreach循环,进行如下所示的较小修改:


And yes you can use for loop for solving your second problem .
also you can use the same foreach loop with minor modification as shown below:

using (Stream s = file.InputStream)
{
  string[] numberOfUsers;
  var sr = new StreamReader(s);
  string contacts = sr.ReadToEnd();
  contacts = contacts.Replace("\r\n", "\n");
  string[] separateLines = contacts.ToString().Split('\n');
  foreach (string line in separateLines)
{
  numberOfUsers = line.Split(',');
  // now you can go through the field
  int count=0;
  foreach( string fieldOnLine in numberOfUsers )
  {
    if(count>0)
    {
    // process fieldOnLine here
    }
    count++;
  }
}


Create a List<Issue> and set values to properties in Issue object. Here is a sample of binding Generic List to GridView:
The TeamCO class:


public class TeamCO
{
 public TeamCO()
 {
 }
    private int _TeamId;
    private string _TeamName;
    public int TeamId
    {
        get { return _TeamId; }
        set { _TeamId = value; }
    }
    public string TeamName
    {
        get { return _TeamName; }
        set { _TeamName = value; }
    }
}
protected void Page_Load(object sender, EventArgs e)
    {
        List<TeamCO> myTeam = new List<TeamCO>();      //create Generic List
        TeamCO t1 = new TeamCO();      //initialize the List
        t1.TeamId = 1000;
        t1.TeamName = "Spurs";
        myTeam.Add(t1);
        TeamCO t2 = new TeamCO();
        t2.TeamId = 2000;
        t2.TeamName = "Lakers";
        myTeam.Add(t2);
        GridView1.DataSource = myTeam;     //bind to GridView
        GridView1.DataBind();
    }


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField HeaderText="NBA">
            <ItemTemplate>
                <%# Eval("TeamID")%>
                <br/>
                <%# Eval("TeamName")%>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
</asp:GridView>
Thanks,


这篇关于我可以将列表作为源传递给gridview吗???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆