使用静态数据绑定网格

本文关键字:网格 数据绑定 静态 | 更新日期: 2023-09-27 18:26:21

我已经编写了如下代码行的Gridview代码

                                                 <div  class="overflowX">
                        <asp:GridView ID="grdView" AutoGenerateColumns="false" BorderWidth="0" runat="server" CssClass="table">
                            <Columns>
                              <asp:TemplateField HeaderText="Non-Resident License">
                                   <ItemTemplate>
                                      <asp:Label ID="lblJurisdiction" runat="server" Text='<%# Container.DataItem %>' />
                                   </ItemTemplate>
                               </asp:TemplateField>

              <asp:TemplateField HeaderText="Effective Date">
                                   <ItemTemplate>
                                    <asp:TextBox ID="txtEffectiveDate" style="padding:12px 5px;" placeholder="(mm/dd/yyyy)" CssClass="datepiker" runat="server"></asp:TextBox>
                            </ItemTemplate>
                               </asp:TemplateField>
                              </Columns>
                        </asp:GridView>
                                   </div>

在cs文件中

private void FillJurisdictionGrid(string Jurisdiction, string IssueDate)
{
    Jurisdiction = "Alabama, Alaska, Arizona";
    IssueDate = "12/10/2015";
    if (Jurisdiction != "")
    {
        grdView.Visible = true;
        grid.Visible = true;
        txtJurisdiction.Enabled = false;
        string[] jurisdictionData = Jurisdiction.Split(',');
        grdView.DataSource = jurisdictionData;
        grdView.DataBind();
    }
    else
    {
        grid.Visible = false;
        grdView.Visible = false;
    }
}

我希望网格看起来像

  • 非居民许可证-生效日期

  • 阿拉巴马州-2015年10月12日

  • 阿拉斯加-2015年10月12日

  • 亚利桑那州-2015年10月12日

我希望日期在网格视图的txtEffective`date文本框中的所有行中显示相同。

请帮帮我。

使用静态数据绑定网格

protected void Page_Load(object sender, EventArgs e)
    {
        string Jurisdiction = "Alabama, Alaska, Arizona";
        string IssueDate = "12/10/2015";
        FillJurisdictionGrid(Jurisdiction, IssueDate);
    }
    private void FillJurisdictionGrid(string Jurisdiction, string IssueDate)
    {

        if (Jurisdiction != "")
        {
            grdView.Visible = true;
            //grid.Visible = true;
            //txtJurisdiction.Enabled = false;
            string[] jurisdictionData = Jurisdiction.Split(',');
            grdView.DataSource = jurisdictionData;
            grdView.DataBind();
            for (int i = 0; i < grdView.Rows.Count; i++)
            {
                TextBox txtEffectiveDate = (TextBox)grdView.Rows[i].FindControl("txtEffectiveDate");
                txtEffectiveDate.Text = IssueDate;//By chance you want to pass the Textbox Value
            }
        }
        else
        {
            //  grid.Visible = false;
            grdView.Visible = false;
        }
    }

试试这个:

` private void FillJurisdictionGrid()
    {
        string Jurisdiction = "Alabama, Alaska, Arizona";
        string IssueDate = "12/10/2015";
        if (Jurisdiction != "")
        {
            grdView.Visible = true;
            string[] jurisdictionData = Jurisdiction.Split(',');
            DataTable dt = new DataTable();
            dt.Columns.Add("Jurisdiction");
            dt.Columns.Add("IssueDate");
            for (int i = 0; i < jurisdictionData.Length; i++)
            {
                DataRow toInsert = dt.NewRow();
                dt.Rows.InsertAt(toInsert, i);
                dt.Rows[i]["Jurisdiction"] = jurisdictionData[i].ToString();
                dt.Rows[i]["IssueDate"] = IssueDate;
            }
            grdView.DataSource = jurisdictionData;
            grdView.DataBind();
        }
        else
        {
            grdView.Visible = false;
        }
    }`