Gridview根据C#变量值绑定列值 [英] Gridview bind the column values based on C# variable value

查看:66
本文介绍了Gridview根据C#变量值绑定列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据所选日期在Exx中动态绑定Date列(例如:Day1,Day2等)。



 <   asp:TemplateField     HeaderText   = 标题 >  
< ItemTemplate >
< asp:Label < span class =code-attribute> ID = LabelRequest runat = server 文字 =' <% #Bind( < span class =code-string> RequestTitle%> ' > < / asp:Label >
< / ItemTemplate >
< ItemStyle 字体粗体 = True Horizo​​ntalAlign = 宽度 = 120px Wrap = True / >

< / asp:TemplateField >
< asp:TemplateField HeaderText = 花费的小时数 ItemStyle-Width = 10% < span class =code-keyword>>
< ItemTemplate >
< asp:TextBox ID = TxtHoursSpent 文字 =' <% #Bind( HoursSpent%> ' runat = server MaxLength = 4 宽度 = 25 > < / asp:TextBox >
< / ItemTemplate >
< ItemStyle 宽度 = 10% < span class =code-keyword>> < / ItemStyle >







我在c#变量中的价值(Ex:TodayDate)。所以该值与列名匹配。我想在aspx 

中添加该值后面的代码Text ='<% #Bind( HoursSpent%> '
)...是它可能???????

请帮助我.........

Thanx:)





这是我的caml查询

qry.Query =
@< 其中 >
< >
< >
< >
< ; Eq >
< FieldRef 名称 =' 资源' / >
< < span class =code-leadattribute>值 类型 =' 整数' >
< UserID / >
< / Value >
< / Eq >
< Eq >
< FieldRef 名称 =' 年' / >
< 价值 类型 =' 文字' > +年份+ @< / Value >
< / Eq >
< /和 > ;
< Eq >
< FieldRef 名称 =' 月' / >
< 类型 =' 文字' < span class =code-keyword>> 0+ TimeSheetMonth + @< / Value >
< / Eq >
< /和 >
< Eq >
< FieldRef 名称 =' 状态' / >
< 类型 =' 选择' > 打开< / Value >
< / Eq >
< /和 >
< /其中 > ;

qry.ViewFields = @< FieldRef 名称 =' ID' / > < FieldRef 名称 =' RequestID' / > < FieldRef 名称 =' 标题' /
> < FieldRef < span class =code-attribute> 名称 =' + columnDay +' / > ;
qry.ViewFieldsOnly = True;
SPListItemCollection listItems = list.GetItems(qry);


如何在aspx中绑定columnDay

解决方案

在您的标记中,您可以执行以下操作:

<% #VariableName % >


  protected   void  GridView1_OnRowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal mycolumn = 7 50 ; // 我相信小时值应为十进制或整数
TextBox txtHrsSpentColumn =(TextBox)e .Row.FindControl( TxtHoursSpent);
txtHrsSpentColumn.Text = mycolumn.ToString();
}
}





请检查这是否是您需要的。

如果mycolumn变量的值来自控件,则将其更改为

  decimal  mycolumn = Convert.ToDecimal (TextBox.Text); 


I want to bind the Date column dynamically in aspx based on the selected Date (Ex: Day1, Day2 etc).

<asp:TemplateField HeaderText="Title">
                      <ItemTemplate>
                        <asp:Label ID="LabelRequest" runat="server" Text='<%# Bind("RequestTitle") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Font-Bold="True" HorizontalAlign="Left" Width="120px" Wrap="True" />

                </asp:TemplateField>
                <asp:TemplateField HeaderText="Hours Spent" ItemStyle-Width="10%">
                    <ItemTemplate>
                        <asp:TextBox ID="TxtHoursSpent" Text='<%# Bind("HoursSpent") %>' runat="server" MaxLength="4" Width="25" ></asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle Width="10%"></ItemStyle>




I've the value for tat in c# variable(Ex: TodayDate). so the value matches the column name. I want to add that code behind value in aspx

Text='<%# Bind("HoursSpent") %>'
)... is it possible???????

Kindly help me.........

Thanx :)



This is my caml query

qry.Query =
                @"<Where>
                              <And>
                                 <And>
                                    <And>
                                       <Eq>
                                          <FieldRef Name='Resource' />
                                          <Value Type='Integer'>
                                             <UserID />
                                          </Value>
                                       </Eq>
                                       <Eq>
                                          <FieldRef Name='Year' />
                                          <Value Type='Text'>" + Year + @"</Value>
                                       </Eq>
                                    </And>
                                    <Eq>
                                       <FieldRef Name='Month' />
                                       <Value Type='Text'>0" + TimeSheetMonth + @"</Value>
                                    </Eq>
                                 </And>
                                 <Eq>
                                    <FieldRef Name='Status' />
                                    <Value Type='Choice'>Open</Value>
                                 </Eq>
                              </And>
                           </Where>";

qry.ViewFields = @"<FieldRef Name='ID' /><FieldRef Name='RequestID' /><FieldRef Name='Title'/><FieldRef Name='" + columnDay + "'/>";
qry.ViewFieldsOnly = True;
SPListItemCollection listItems = list.GetItems(qry);


How can i bind the "columnDay" in aspx

解决方案

In your markup you can do this:

<%# VariableName %>


protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         decimal mycolumn= 7.50; //i believe hours value should be decimal or int
         TextBox txtHrsSpentColumn = (TextBox)e.Row.FindControl("TxtHoursSpent");
         txtHrsSpentColumn.Text = mycolumn.ToString(); 
     }
}



Please check if this is what you need.
if the value of mycolumn variable is coming from a control change it to

decimal mycolumn = Convert.ToDecimal(TextBox.Text);


这篇关于Gridview根据C#变量值绑定列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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