问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

怎样将LIST中的数据绑定到GridView,并实现增删改查

发布网友 发布时间:2022-04-07 23:34

我来回答

4个回答

懂视网 时间:2022-04-08 03:55

<GridView.ColumnHeaderTemplate>
     <DataTemplate>
          <TextBlock FontFamily="Microsoft YaHei" FontSize="35" Foreground="White" Background="#333" Width="265" HorizontalAlignment="Center" TextAlignment="Center">
            <TextBlock.Text>
                  <Binding/>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
</GridView.ColumnHeaderTemplate>

 

另外还有一个重要的模板,就是CellTemplate,我们需要注意GridViewColumn用了DisplayMemberBinding 使得CellTemplate失效,所以如果需要自定义CellTemplate,那么一定要去掉 DisplayMemberBinding。 下面是一个具体的例子。
首先在资源中定义模板,

<DataTemplate x:Key="GridViewCellTemplate">
      <TextBlock FontFamily="Microsoft YaHei" Height="60" FontSize="35" Foreground="White" Background="Gray" Width="265" HorizontalAlignment="Center" TextAlignment="Center">
          <TextBlock.Text>
                <Binding Path="DisplayName" Mode="TwoWay"/>
          </TextBlock.Text>
      </TextBlock>
</DataTemplate>

然后在具体的列中引用,<GridViewColumn Header="投诉人"   Width="Auto" CellTemplate="{StaticResource GridViewCellTemplate}" ></GridViewColumn>这样就可以实现对每一列的模板替换。

还有很重要的一部分就是在后台来讲数据库绑定到前台中,在xaml中我们使用了ItemsSource="{Binding}",那么在后台的代码中:  

/// <summary>
/// 从数据库中获取数据并绑定到GridView上
/// </summary>
private void GetDataFromDataBase()
{
string connectStr = "Server=127.0.0.1;Database=test;Uid=root;Pwd=12345;Port=3306;CharSet=gbk;allow zero datetime=true";
string cmdStr = "select * from camerainfo order by pID desc limit 100";
DataBaseMySqlHander sql = new DataBaseMySqlHander(connectStr);
DataTable dt = sql.GetDataTable(cmdStr);
if (dt == null) return;
//创建一个DataTable的副本
DataTable tempTable = new DataTable();
tempTable = dt.Clone();//克隆 System.Data.DataTable 的结构
//包括所有 System.Data.DataTable 架构和约束。
if (tempTable != null && dt.Rows.Count>0)
{
     for (int i = 0; i < dt.Rows.Count; i++)
       {
           tempTable.ImportRow(dt.Rows[i]);
       }
}
//为ListView绑定数据源
this.dv= tempTable.DefaultView;
tempTable.Dispose();
}

以上部分就实现了简单的数据库的绑定,这部分也是非常重要的部分,我们在使用的时候可以按照上述步骤来进行绑定!

WPF将数据库和GridView绑定并更改GridView模板

标签:

热心网友 时间:2022-04-08 01:03

我的一个GridView操作文件,数据源是实体LIST,删改查功能都有了,你参考下
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using lwf_MVC.CTRL;
using lwf_MVC.lwf_Normal;
using lwf_MVC.MODEL;

public partial class admin_news_edit : System.Web.UI.Page
{
public string icls_name = "cls_f_news";

//不同级数系列区别在于iWhere
public string iWhere = " 1 = 1 and ENG = " + WEB.GQ("ENG", "0") + " and TID1 = " + WEB.GQ("SSS", "1") + " and 1 = 1";

public int getRecordCount()
{

cls_f_news if_news = (cls_f_news)CTRL.CreateModel(icls_name);
if_news.title = soKey.Text;
if_news.contents = soKey.Text;
if_news.WHERE = iWhere;
int i_recordcount = CTRL.getCount(if_news, "order by orderid,id desc");
return i_recordcount;

}

protected void Page_Load(object sender, EventArgs e)
{
if (WEB.getSession("userName") == null || WEB.getSession("userName") == "") JS.RD(System.Web.HttpUtility.UrlDecode("%e7%99%bb%e9%99%86%e8%b6%85%e6%97%b6%2c%e8%af%b7%e9%87%8d%e6%96%b0%e7%99%bb").Trim(), "../login.aspx");
if (!IsPostBack)
{
AspNetPagerBin(1);
GridViewBin();
}
}

protected void AspNetPagerBin(int curr)
{

int i_recordcount = getRecordCount();
AspNetPager1.RecordCount = i_recordcount;
AspNetPager1.CurrentPageIndex = curr;

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBin();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBin();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int i_id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);

baseTableObj.title = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
baseTableObj.newfrom = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
baseTableObj.addTime = DateTime.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());

baseTableObj.orderid = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].FindControl("tb_orderid_b")).Text.ToString());

if (CTRL.Update(baseTableObj))
{
JS.alert(System.Web.HttpUtility.UrlDecode("%e4%bf%ae%e6%94%b9%e6%88%90%e5%8a%9f").Trim());
}
else
{
JS.alert(System.Web.HttpUtility.UrlDecode("%e7%bd%91%e7%bb%9c%e8%b6%85%e6%97%b6%ef%bc%8c%e8%af%b7%e9%87%8d%e6%96%b0%e5%86%8d%e8%af%95").Trim());
}

//====================================

GridView1.EditIndex = -1;
GridViewBin();

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int i_id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
if (CTRL.Delete(baseTableObj))
{
JS.alert(System.Web.HttpUtility.UrlDecode("%e8%ae%b0%e5%bd%95%e5%88%a0%e9%99%a4%e6%88%90%e5%8a%9f").Trim());
}
else
{
JS.alert(System.Web.HttpUtility.UrlDecode("%e7%bd%91%e7%bb%9c%e8%b6%85%e6%97%b6%ef%bc%8c%e8%af%b7%e9%87%8d%e6%96%b0%e5%86%8d%e8%af%95").Trim());
}
//====================================

GridView1.EditIndex = -1;
GridViewBin();
}

protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
GridView1.PageIndex = AspNetPager1.CurrentPageIndex;
GridViewBin();
}

protected void CheckBox_all_CheckedChanged(object sender, EventArgs e)
{
CheckBox_opp.Checked = false;

for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
{
CheckBox ckb = (CheckBox)GridView1.Rows[i_i].FindControl("cb_selBoxs");
if (CheckBox_all.Checked)
{
ckb.Checked = true;
}
else
{
ckb.Checked = false;
}
}
}

protected void CheckBox_opp_CheckedChanged(object sender, EventArgs e)
{
CheckBox_all.Checked = false;

for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
{
CheckBox ckb = (CheckBox)GridView1.Rows[i_i].FindControl("cb_selBoxs");
if (ckb.Checked)
{
ckb.Checked = false;
}
else
{
ckb.Checked = true;
}

}
}

protected void lb_delete_Click(object sender, EventArgs e)
{
for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
{
CheckBox ckb = (CheckBox)GridView1.Rows[i_i].FindControl("cb_selBoxs");
if (ckb.Checked)
{

int i_id = int.Parse(GridView1.DataKeys[i_i].Value.ToString());
cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
CTRL.Delete(baseTableObj);

}
}

int i_Pcount = getRecordCount();

if (AspNetPager1.CurrentPageIndex > i_Pcount)
{
AspNetPagerBin(i_Pcount);
}
else
{
AspNetPagerBin(AspNetPager1.CurrentPageIndex);
}
GridViewBin();
}
protected void lb_order_Click(object sender, EventArgs e)
{
for (int i_i = 0; i_i <= GridView1.Rows.Count - 1; i_i++)
{
TextBox tb = (TextBox)GridView1.Rows[i_i].FindControl("tb_orderid_a");

int i_id = int.Parse(GridView1.DataKeys[i_i].Value.ToString());
cls_f_news baseTableObj = (cls_f_news)CTRL.CreateModel(icls_name, i_id);
baseTableObj.orderid = int.Parse(tb.Text.ToString());
CTRL.Update(baseTableObj);

}
GridViewBin();
}
protected void bt_search_Click(object sender, EventArgs e)
{
AspNetPagerBin(1);
GridViewBin();
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowNum;
int id_i;

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

}
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;

ViewState["sortExpression"] = sortExpression;

if (ViewState["ASC"].ToString() == "0")
{
ViewState["ASC"] = "1";
}
else
{
ViewState["ASC"] = "0";
}

AspNetPagerBin(1);
GridViewBin();
}

protected void GridViewBin()
{

CheckBox_all.Checked = false;

cls_f_news if_news = (cls_f_news)CTRL.CreateModel(icls_name);
if_news.title = soKey.Text;
if_news.contents = soKey.Text;
if_news.WHERE = iWhere;

int RecordCount;
int curP = Convert.ToInt32(AspNetPager1.CurrentPageIndex);
List<cls_f_news> iList_f_news = CTRL.getPageList(if_news, "order by orderid,id desc", AspNetPager1.PageSize, curP, out RecordCount).ConvertAll<cls_f_news>(delegate(lwf_MVC.MODEL.baseTable s) { return (cls_f_news)s; });

if (ViewState["ASC"] == null) ViewState["ASC"] = "-1";
if (ViewState["sortExpression"] == null) ViewState["sortExpression"] = "orderid";
if (ViewState["ASC"].ToString() == "-1")
{
iList_f_news.Sort(delegate(cls_f_news a, cls_f_news b) { return (new CaseInsensitiveComparer()).Compare(a.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(a, null), b.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(b, null)); });
}
else
{
if (ViewState["ASC"].ToString() == "1")
{
iList_f_news.Sort(delegate(cls_f_news a, cls_f_news b) { return (new CaseInsensitiveComparer()).Compare(a.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(a, null), b.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(b, null)); });
}
else
{
iList_f_news.Sort(delegate(cls_f_news a, cls_f_news b) { return (new CaseInsensitiveComparer()).Compare(b.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(b, null), a.GetType().GetProperty(ViewState["sortExpression"].ToString()).GetValue(a, null)); });

}

}

GridView1.DataSource = iList_f_news;
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
}
}

热心网友 时间:2022-04-08 02:21

给你个很笨的办法,在页面调用后台的方法,获取数据,显示

热心网友 时间:2022-04-08 03:56

装箱,装到datatable中去
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
求电脑玩安卓游戏的方法。步骤。以及所有的文件~谢谢 。有的联系我~ 我在电脑管家软件管理下的鹰击长空2都是解压包 澳洲留学读研申请攻略 澳洲留学申请时间是什么时候 去澳大利亚上大学需要什么手续 澳洲留学申请流程时间如何? 中南财经政法大学保险精算专业介绍 黄福记小吃梅干菜扣肉饼怎么样 如何开一家梅干菜扣肉饼店,成本要多少 在照明行业LED配光曲线图怎么看 csm醒枪数值跳 格连csm重量 乐视tv电视打开第三方视频网站怎么调清晰度 乐视网看电影时如何调清晰度 乐视电视机连接电脑怎么样清晰吗 我的电视是乐视的分辨率1920*1080,看直播电视盒子需要设置1080i分辨率还是1080p分辨 乐视电视的有线电视不清晰? 乐视超级电视的质量清晰度怎样 乐视一体机在哪调分辨率 乐视怎么调成流畅,清晰度一直是超清没法改 乐视TV连接机顶盒后,看电视画面不清晰,怎么调试? 百家讲坛观后感,大家帮帮忙啊,老师要求的作业 语文老师让不少于600字的阅读展示(上讲台交流),是有关《骆驼祥子》的,和读后感差不多,帮忙想个开头 动漫《西部世界》第二季第4集免费百度云资源下载观看 超神学院之雄兵连第二季的第5集免费在线观看 我的前半生第二部61集免费观看在哪个卫视 为了为鼓励同学不限于书上的知识,动手操作获得感性认识老师在实验室前写上了两句诗—————— 百家讲台的唐宋八大家怎么讲到柳宗元就不讲了? 百家讲坛辛弃疾 asp.net数据库怎么绑定gridview?我要把某个数据库里的所有表的表名和gridview绑定,代码怎么写? 苹果手机上的爱思极度版可以改变微信共享实时定位地址嘛 老款和新款的纪梵希散粉1号色哪个好看? 4399神将世界69级周瑜怎么过夏候□ 怎样才让自己的中文打字速度上120字&#47;分钟?我想知道秘诀及具体的方法?希望能得到完整的解答,谢谢! 初二寒假作文大话年俗 那位能介绍一个练中文打字的软件 win7旗舰版屏幕键盘怎么打开 电脑中文打字技巧 小学生练习打字的软件有哪些?最基础的那种.. 有什么好看的欧美电影吗?? 在梦幻西游里一天做了100多个师门会怎么样? 梦幻西游 69HS 比武 组什么队 火王之千里同风里童风使用的淡蓝色手机是什么牌子? 五年级下册分数加减混合脱式计算题一百道及答案,简单点的 古代人怎么称呼自己的妹妹? 文言文里兄弟妹姐怎么称呼 古文的称呼 文言文中代表你的称呼 我的妹妹用文言文怎么说 米兰花的花语是什么