如何添加一个空的未绑定的评论列到GridView? [英] How to Add an Empty, Unbound, Comment Column to a GridView?

查看:88
本文介绍了如何添加一个空的未绑定的评论列到GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.net中放置了一个GridView表格,允许用户批准一系列记录。作为审批流程的一部分,我想提供一个空栏供用户在必要时提供评论。显示的唯一记录将是尚未批准的记录,因此,不需要加载注释。一旦获得批准,用户将无法再次查看这些记录。



如何将这个空列添加到我的GridView中?再次,没有数据要加载到此列中。一旦获得批准,SQL Server表中的记录将更新为时间戳和注释。



到目前为止,我在这里找到的大部分内容都与添加空行,但不添加列。我完全是新的ASP.net,所以任何帮助将不胜感激。



当前列代码:

  ASP:模板列的HeaderText = 注释 ItemStyle-WIDTH = 500像素 ItemStyle-WRAP = 真 的SortExpression = 注释/ 


解决方案

我很快就把一个简单的解决方案放在一起。它使用GridView的 OnRowCommand 事件。

 < asp: GridView ID =GridView1runat =serverOnRowCommand =GridView1_RowCommand> 
<列>
< asp:TemplateField HeaderText =TextBox>
< ItemTemplate>
将ASP:文本框ID = TextBox1中 RUNAT = 服务器 的TextMode = 多行 文本=<%#的eval( 文本框)%>>< / ASP:文本框>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =UpdateButton>
< ItemTemplate>
< asp:Button ID =Button1runat =serverText =Update/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

后面的代码

  protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
//将发送方转换回gridview
GridView gv = sender as GridView;

//将命令源转换回按钮
按钮btn = e.CommandSource作为Button;

//将按钮的namingcontainer转换回gridviewrow
GridViewRow row = btn.NamingContainer as GridViewRow;

//使用的FindControl和从行
文本框TB得到的索引找到正确的文本框= gv.Rows [row.DataItemIndex] .FindControl( TextBox1中)作为文本框;

//显示结果
Label1.Text = tb.Text;
}

更新
$ b

或者如果您想通过按下按钮一次更新所有记录。

  protected void Button1_Click (对象发件人,EventArgs的)
{
的foreach(在GridView1.Rows GridViewRow行)
{
文本框TB = row.FindControl( TextBox1中)作为文本框;
Label1.Text + = tb.Text +< br>;
}
}


I've put together a GridView table in ASP.net that allows users to approve a series of records. As part of this approval process, I'd like to provide an empty column for the user to provide comments where necessary. The only records showing up will be records that haven't been approved yet, thus, no comments will need to be loaded. Once they are approved, users will not be able to view these records again.

How do I go about adding this empty column to my GridView? Again, there's no data to load into this column. Once approved, the records in the SQL Server table will be updated with a timestamp and comments.

Most of what I was able to find on here so far has been related to adding empty rows, but not columns. I'm completely new to ASP.net, so any help would be greatly appreciated.

Current Column Code:

asp:TemplateField HeaderText="Comment" ItemStyle-Width="500px" ItemStyle-Wrap="true" SortExpression="Comment" /

解决方案

Just a simple solution I've quickly put together. It uses the OnRowCommand event of the GridView.

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="TextBox">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Text='<%# Eval("textfield") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="UpdateButton">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Update" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code behind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //cast the sender back to a gridview
    GridView gv = sender as GridView;

    //cast the commandsource back to a button
    Button btn = e.CommandSource as Button;

    //cast the namingcontainer of the button back to a gridviewrow
    GridViewRow row = btn.NamingContainer as GridViewRow;

    //find the correct textbox using findcontrol and the index obtained from the row
    TextBox tb = gv.Rows[row.DataItemIndex].FindControl("TextBox1") as TextBox;

    //show result
    Label1.Text = tb.Text;
}

UPDATE

Or if you want to update all the records at once by pressing a button.

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        TextBox tb = row.FindControl("TextBox1") as TextBox;
        Label1.Text += tb.Text + "<br>";
    }
}

这篇关于如何添加一个空的未绑定的评论列到GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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