1.å¦ä½ç¨C#åè´ç©è½¦
2.请大神打救啊!把下面的ASP.NET代码逐行注释一下。
3.C#用datagriview控件增加删除行数据,并且更新到xml中,求代码(急)如图
4.C# 如何创建数据表结构
å¦ä½ç¨C#åè´ç©è½¦
é¦å ï¼ä¸ºæ¯ä¸ä¸ªå®¢æ·çæä¸ä¸ªèæçè´ç©è½¦ï¼å ¶å®å°±æ¯ä¸ä¸ªDatatableï¼
DataTable GoodsCar = new DataTable();
GoodsCar.Columns.Add("Index",资料源码领取typeof(int));//åååºå·
ä¸é¢ä½ è¿å¯ä»¥å å¾å¤çï¼æ¯å¦ååå称ï¼ä»·æ ¼ï¼äº§å°çç
ç¶åï¼å¨å·²ç»åå¨çååæ éææéæ©çåååºå·å å ¥è´ç©è½¦ä¸ï¼å°±æ¯å¾Datatableä¸å¢å æéæ©çè¡ä¿¡æ¯ï¼
DataRow ChoosedGoods = GoodSCar.NewRow();
ChoosedGoods["Index"] = ä½ å·²ç»åå¨çåååºåå·;
æåï¼éæ©å®äºç»å¸åï¼åºè¯¥æä¸ä¸ªç»¼åæ¾ç¤ºå·²ç»éæ©çååï¼å°±æ¯ç°å®è¡¨GoodsCarä¸çä¿¡æ¯ï¼
çæä¸ä¸ªDatalistæè DataGridç¶åç»å®ï¼æ¾ç¤ºåºæ¥ãè¿å¯ä»¥å¾éé¢å å ¥ä¸ç¹å«çåè½ï¼æ¯å¦ï¼å é¤å·²ç»éæ©çç©åççï¼
è¿ä¸ªç±»ä¼¼çç¨åºæºä»£ç ç½ä¸å¾å¤çãé便ä»ç¾åº¦ä¸æä¸ä¸é½æ¯ä¸å¤§æåï¼
请大神打救啊!把下面的源码要怎么安装ASP.NET代码逐行注释一下。
下面我来简单的说一下,我在源代码中进行逐行的进行了解释,下面贴一下代码: /// <summary>/// 显示最新条房源信息
/// </summary>
protected voidBindNewHouseinfo()
{
//设置模块头信息
this.LabelTitle.Text ="最新房源";
//获取最新房源信息,具体的方法在数据访问层封装(这个无非是查询到attr="new"最新的或者根据发布时间查出)
DataTable dt = newBLL.HouseInfoManager().SelectNewHouseinfo();
//创建分页数据对象
PagedDataSource Pds =new PagedDataSource();
Pds.DataSource =dt.DefaultView; //指定数据源
Pds.AllowPaging =true; //允许分页
Pds.PageSize = 3; //分页条数
Pds.CurrentPageIndex =Convert.ToInt(this.lbNown.Text.ToString()) - 1; //指定当前页码
this.lbAlln.Text =Pds.PageCount.ToString(); //显示总页数
lkbHomen.Enabled =true; //显示第一页
lkbPren.Enabled =true; //显示上一页
lkbNextn.Enabled =true; //显示下一页
lkbLastn.Enabled = true; //显示最后一页
//如果当前页已经是第一页,则隐藏第一页和上一页
if (Convert.ToInt(this.lbNown.Text.ToString()) <= 1)
{
this.lkbHomen.Enabled = false;
this.lkbPren.Enabled = false;
}
//如果当前页已经是最后一页,则隐藏最后一页和下一页
if (Convert.ToInt(this.lbNown.Text.ToString()) > Pds.PageCount- 1)
{
this.lkbNextn.Enabled = false;
this.lkbLastn.Enabled = false;
}
//不明所以,可能是网站源码淘宝交易一个标志标签
this.lb.Visible =false;
//将分页数据绑定到DataList控件
this.dlAllHouseInfo.DataSource = Pds;
this.dlAllHouseInfo.DataBind();
}
哎呀,真佩服我的耐心,终于解释完了,希望能给你帮助。
C#用datagriview控件增加删除行数据,元神瞬移源码并且更新到xml中,求代码(急)如图
完全按你的要求写的,供参考:前提是XML文件已创建好,在窗体里选择该XML文件,红包扫雷正版源码再行操作:
窗体:
//选择XML按钮private void btnXML_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
OpenFileDialog xmlFile = new OpenFileDialog();
xmlFile.Filter = "*.xml|";
xmlFile.Title = "请选择xml文件";
if (xmlFile.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = xmlFile.FileName;//在textBox1控件中显示选择的路径
dt=GetXML(this.txtPath.Text);
}
if(!dt.Equals(null)&&dt.Rows.Count>0)
LoadData(dt, dgvXML);//加载到窗体中
}
//保存按钮
private void button1_Click(object sender, EventArgs e)
{
int n = SaveDgv();
if (n > 0)
{
MessageBox.Show("保存成功!");
DataTable dt = new DataTable();
dt = GetXML(this.txtPath.Text);
LoadData(dt, dgvXML);//刷新加载
}
else
{
MessageBox.Show("保存失败!");
DataTable dt = new DataTable();
dt = GetXML(this.txtPath.Text);
LoadData(dt, dgvXML);//刷新加载
}
}
//删除按钮
private void btnDelete_Click(object sender, EventArgs e)
{
int n = 0;//判断是否删除
for (int i = 0; i < dgvXML.SelectedRows.Count; i++)
{
string id = dgvXML.SelectedRows[i].Cells[0].Value.ToString();
XmlDocument doc = new XmlDocument();
doc.Load(this.txtPath.Text);
XmlNodeList nod = doc.DocumentElement.ChildNodes;
XmlNode root = doc.DocumentElement;
foreach (XmlNode peo in nod)
{
if (XmlNode.Equals(peo.SelectSingleNode("ID").InnerText, id))
{
root.RemoveChild(peo);//从根节点删除该ID的PERSON
doc.Save(this.txtPath.Text);
n++;
}
}
}
if (n > 0)
{
DataTable dt = new DataTable();
dt = GetXML(this.txtPath.Text);
LoadData(dt, dgvXML);//刷新加载
MessageBox.Show("删除成功!");
}
else
{
MessageBox.Show("删除失败!");
}
}
// DataGridView中加载数据
private void LoadData(DataTable dt, DataGridView dgv)
{
dgv.Rows.Clear();//datagridview清空数据
if (dt != null && dt.Rows.Count > 0)
{
dgv.Rows.Add(dt.Rows.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
dgv.Rows[i].Cells[k].Value = dt.Rows[i][k].ToString();//赋值
}
}
}
}
//datagridview添加行号
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
e.Row.HeaderCell.Value = (e.Row.Index + 1).ToString();
}
//解析XML生成DATATABLE
private List<string> IDList = new List<string>();//用来记录已有的ID,以在修改时判断是否新增
private DataTable GetXML(string path)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
dt.Columns.Add("SEX");
dt.Columns.Add("PHONE");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nod = doc.DocumentElement.ChildNodes;
foreach (XmlNode peo in nod)
{
XmlNodeList pe = peo.ChildNodes;
DataRow row = dt.NewRow();
foreach (XmlNode p in pe)
{
if (p.Name == "ID")
{
row["ID"] = p.InnerText.ToString();
IDList.Add(p.InnerText.ToString());//添加ID记录,以在修改时判断是否新增
}
if (p.Name == "NAME")
{
row["NAME"] = p.InnerText.ToString();
}
if (p.Name == "SEX")
{
row["SEX"] = p.InnerText.ToString();
}
if (p.Name == "PHONE")
{
row["PHONE"] = p.InnerText.ToString();
}
}
dt.Rows.Add(row);
}
return dt;
}
//修改dgv数据后保存到XML文件
private int SaveDgv()
{
int n = 0;
for (int i = 0; i < dgvXML.Rows.Count-1; i++)
{
XmlDocument doc = new XmlDocument();
doc.Load(this.txtPath.Text);
XmlNodeList nod = doc.DocumentElement.ChildNodes;
string id = dgvXML.Rows[i].Cells[0].Value.ToString();
if (IDList.Contains(id)) //如果是已有的ID,则修改
{
foreach (XmlNode peo in nod)
{
if (XmlNode.Equals(peo.SelectSingleNode("ID").InnerText, id))
{
peo.SelectSingleNode("NAME").InnerText = dgvXML.Rows[i].Cells[1].Value.ToString();//赋值姓名
peo.SelectSingleNode("SEX").InnerText = dgvXML.Rows[i].Cells[2].Value.ToString();//赋值性别
peo.SelectSingleNode("PHONE").InnerText = dgvXML.Rows[i].Cells[3].Value.ToString();//赋值电话
doc.Save(this.txtPath.Text);
n++;
break;//当找到该ID记录并修改后,跳出循环
}
}
}
else //否则为新增
{
XmlNode root = doc.DocumentElement;
XmlElement PERSON = doc.CreateElement("PERSON");
XmlElement ID = doc.CreateElement("ID");
ID.InnerText = id;//赋值id
XmlElement NAME = doc.CreateElement("NAME");
NAME.InnerText = dgvXML.Rows[i].Cells[1].Value.ToString();//赋值姓名
XmlElement SEX = doc.CreateElement("SEX");
SEX.InnerText = dgvXML.Rows[i].Cells[2].Value.ToString();//赋值性别
XmlElement PHONE = doc.CreateElement("PHONE");
PHONE.InnerText = dgvXML.Rows[i].Cells[3].Value.ToString();//赋值电话
PERSON.AppendChild(ID);
PERSON.AppendChild(NAME);
PERSON.AppendChild(SEX);
PERSON.AppendChild(PHONE);
root.AppendChild(PERSON);
doc.Save(this.txtPath.Text);//保存
n++;
}
}
return n;
}
XML文件:
<?xml version="1.0" standalone="yes"?>
<List>
<PERSON>
<ID></ID>
<NAME>测一</NAME>
<SEX>男男</SEX>
<PHONE></PHONE>
</PERSON>
<PERSON>
<ID></ID>
<NAME>再二</NAME>
<SEX>女女</SEX>
<PHONE></PHONE>
</PERSON>
<PERSON>
<ID></ID>
<NAME>张三</NAME>
<SEX>男男</SEX>
<PHONE></PHONE>
</PERSON>
<PERSON>
<ID></ID>
<NAME>王五</NAME>
<SEX>女女</SEX>
<PHONE></PHONE>
</PERSON>
</List>
C# 如何创建数据表结构
下面的示例创建具有 DataColumn 和 DataRow 的新 DataTable,并在 DataGridView 控件中显示它。
private void MakeDataTableAndDisplay()
{
// Create new DataTable.
DataTable table = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType, ColumnName
// and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int");
column.ColumnName = "id";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "item";
table.Columns.Add(column);
// Create new DataRow objects and add to DataTable.
for(int i = 0; i < ; i++)
{
row = table.NewRow();
row["id"] = i;
row["item"] = "item " + i;
table.Rows.Add(row);
}
// Set to DataGrid.DataSource property to the table.
dataGrid1.DataSource = table;
}
这是MSDN里面的源代码,就是创建一个数据表结构的。
其实我们平时遇到的很多问题,大多可以在MSDN里面查找到。
2024-11-19 02:09
2024-11-19 02:04
2024-11-19 01:59
2024-11-19 01:58
2024-11-19 01:57
2024-11-19 01:29
2024-11-19 00:26
2024-11-19 00:17