获得一个对象返回从我的GridView行 [英] Getting an object back from my GridView rows

查看:84
本文介绍了获得一个对象返回从我的GridView行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想我的对象回来...

Basically, i want my object back...

我有一个电子邮件的对象。

I have an Email object.

public class Email{
    public string emailAddress;
    public bool primary;
    public int contactPoint;
    public int databasePrimaryKey;

    public Email(){}
}

在我的用户,我的电子邮件对象的列表。

In my usercontrol, i a list of Email objects.

public List<Email> EmailCollection;

而且我我的用户里面绑定这一个GridView。

And i'm binding this to a GridView inside my usercontrol.

if(this.EmailCollection.Count > 0){
    this.GridView1.DataSource = EmailCollection;
    this.GridView1.DataBind();
}

这将是真正真棒,如果我能得到一个Email对象后面的出在GridView的的版本。

我如何做到这一点?

我也只是结合了一些电子邮件对象的属性到GridView,以及和他们投入项目模板。

I'm also binding only some of the Email object's properties to the GridView as well and they're put into Item Templates.

<Columns>

<asp:TemplateField HeaderText="Email Address">
    <ItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text=<%# Eval("EmailAddress") %> Width=250px />
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Primary">
    <ItemTemplate>
        <asp:CheckBox runat="server" Checked=<%# Eval("PrimaryEmail") %> />
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Contact Point">
    <ItemTemplate>
        <CRM:QualDropDown runat="server" Type=ContactPoint InitialValue=<%# Eval("ContactPoint") %> />
    </ItemTemplate>
</asp:TemplateField>

</Columns>

能否GridView控件甚至做到这一点?我是否需要推出自己的东西吗?这会是真的很酷如果它会为我做的。

Can GridView even do this? Do i need to roll my own thing? It'd be really cool if it would do it for me.


要详细说明了。

我保存列表集合到视图状态。

I am saving the List collection into the viewstate.

什么我最终试图去,是会有一个保存按钮某处控制,事件触发时,我想创建在GridView一个DataRow要比较一个Email对象,我原来List集合。这时如果有一个变化,那么我会在数据库中更新该行。我在想,如果我可以把一个List集合到一个GridView,那么也许我能得到它的权利退了出去。

What I'm eventually trying to get to, is there will be a Save button somewhere in the control, which when the event fires I'd like to create an Email object from a datarow in the GridView which to compare to my original List collection. Then if there's a change, then I'd update that row in the database. I was thinking that if I could put a List collection into a GridView, then perhaps I could get it right back out.

也许我创建一个新的构造我的电子邮件对象,它需要一个DataRow?但后来有很多复杂的是进入了...

Perhaps I create a new constructor for my Email object which takes a DataRow? But then there's a lot of complexities that goes into that...

推荐答案

好吧,我结束了在我的名单EmailCollection,这是保存到ViewState中循环。

Well I ended up looping through my List EmailCollection, which was saved into the ViewState.

因此​​,在该页面,保存按钮被点击,当事件被抓住了,我依次通过我的列表收集和索引抢在GridView行。

So in the page, a Save button is clicked, when the event is caught, I loop through my List Collection and grab the row from the GridView by index.

在GridViewRow我必须使用GridView1.Rows [I] .Cells [J] .FindControl(myControl1),然后从它那里得到相应的价值,是一个复选框,文本框或下拉列表

On the GridViewRow I have to use a GridView1.Rows[i].Cells[j].FindControl("myControl1") then get the appropriate value from it, be it a check box, text box, or drop down list.

我看到一个GridViewRow对象有一个DataItem属性,它包含我的Email对象,但它在RowBound阶段是唯一可用的。

I do see that a GridViewRow object has a DataItem property, which contains my Email object, but it's only available during the RowBound phase.

不幸的是,如果/当我需要在这个电子邮件收集后扩大,通过添加或删除列,它会采取几个步骤。

Unfortunately If/When i need to expand upon this Email Collection later, by adding or removing columns, it'll take a few steps.

protected void SaveButton_OnClick(object sender, EventArgs e){
    for (int i = 0; i < this.EmailCollection.Count; i++)
    {
        Email email = this.EmailCollection[i];
        GridViewRow row = this.GridView1.Rows[i];

        string gv_emailAddress = ((TextBox)row.Cells[0].FindControl("EmailAddress")).Text;
        if (email.EmailAddress != gv_emailAddress)
        {
            email.EmailAddress = gv_emailAddress;
            email.Updated = true;
        }
        ...
    }
}

我仍然是开放的更有效的解决方案。

I'd still be open to more efficient solutions.

这篇关于获得一个对象返回从我的GridView行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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