1.showdialogԴ?源码?
2.C#å°å¾çåç´ åä½
showdialogԴ??
完全按你的要求写的,供参考:前提是源码XML文件已创建好,在窗体里选择该XML文件,源码织梦源码个人网页源码zip版再行操作:
窗体:
//选择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,源码spdif源码 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#å°å¾çåç´ åä½
å°±æ¯æ¹åä¸å¼ å¾çç大å°åï¼å®ä¹å ¨å±çåéï¼ private Image curImage = null;
private string ImageFileName = null;ä¸é¢çå®ä¹å¨ä¸ä¸ªæé®éé¢ï¼ç¨æ¥æå¼å¾çã//å½ç¶ä½ è¦æå¼å¾çæè¡åã OpenFileDialog OpenDlg = new OpenFileDialog(); if (OpenDlg.ShowDialog() == DialogResult.OK)
{
ImageFileName = OpenDlg.FileName;
curImage = Image.FromFile(ImageFileName);
}åå¨å¦å¤çæé®ä¸é¢å建ä¸å¼ å¾çï¼ç¨æ¥ä¿åæ°å¾çãBitmap bitmap=new Bitmap(,);bitmap.Save("xx.jpg",ImageFormat.Jpeg); //è¿æ ·ä¿åå¨å½åç®å½ï¼å¹¶ä¸æå®äºåªè½æ¯è¿ä¸ªæ ¼å¼çï¼å½ç¶å¯ä»¥è®¾ç½®å ¶ä»æ ¼å¼çï¼ä¸é¢æå®æ´ä»£ç ã//////////å ³äºååå¾ç大å°ã///////using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;namespace 使ç¨ä¸å大å°ä¿åå¾ç1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private Image curImage = null;
private string ImageFileName = null;
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog OpenDlg = new OpenFileDialog();
OpenDlg.Filter = "All ImageFile|*.jpg;*.bmp;*.gif;*.png|(*.bmp)|*.bmp|(*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif";
OpenDlg.Title = "Open Image File"; if (OpenDlg.ShowDialog() == DialogResult.OK)
{
ImageFileName = OpenDlg.FileName;
curImage = Image.FromFile(ImageFileName);
}
this.Invalidate();
} private void Form1_Paint(object sender, PaintEventArgs e)
{
if (curImage != null)
{
e.Graphics.DrawImage(curImage, 0, 0);
}
} private void button1_Click(object sender, EventArgs e)
{
try
{
if (this.textBox1.Text == "" || this.textBox2.Text == "")
{
MessageBox.Show("请è¾å ¥å¤§å°å®æ´");
}
else
{
int width = Convert.ToInt(textBox1.Text);
int height = Convert.ToInt(textBox2.Text); SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "(All ImageFile)|*.jpg;*.png;*.gif;*.bmp";
saveDlg.Title = "Save ImageFile";
saveDlg.OverwritePrompt = true;
if (saveDlg.ShowDialog() == DialogResult.OK)
{
string filename=saveDlg.FileName;
string extn = filename.Remove(0, saveDlg.FileName.Length - 3); Bitmap newBitmap = new Bitmap(curImage, new Size(width, height)); if (extn.Equals("jpg"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Jpeg);
else if (extn.Equals("png"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Png);
else if (extn.Equals("bmp"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Bmp);
else if (extn.Equals("gif"))
newBitmap.Save(saveDlg.FileName, ImageFormat.Gif);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}ãããããããããããããããä¸é¢è®¾ç½®ç大å°æ¯éè¿ä½¿ç¨äºï¼ä¸¤ä¸ªTextBoxæ¥è®¾ç½®çã设计代ç (ç¥)ã