private void dgvDevice_CellClick(object sender, DataGridViewCellEventArgs e) { // Cai dat cau hinh if (e.ColumnIndex == 5) { int index = e.RowIndex; SettingDevice settingDevice = new SettingDevice(); settingDevice.DeviceName = dgvDevice.Rows[index].Cells[1].Value.ToString(); settingDevice.Cops = dgvDevice.Rows[index].Cells[2].Value.ToString(); settingDevice.DelayTime = Int32.Parse(dgvDevice.Rows[index].Cells[3].Value.ToString()); settingDevice.Limit = Int32.Parse(dgvDevice.Rows[index].Cells[4].Value.ToString()); SetupDeviceForm deviceSetupForm = new SetupDeviceForm(); deviceSetupForm.dataSetup = settingDevice; deviceSetupForm.ShowDialog(); dgvDevice.Rows[index].Cells[2].Value = deviceSetupForm.cbMangDiDong.SelectedValue.ToString(); dgvDevice.Rows[index].Cells[3].Value = deviceSetupForm.cbDoTre.SelectedValue.ToString(); dgvDevice.Rows[index].Cells[4].Value = deviceSetupForm.cbGioiHan.SelectedValue.ToString(); dgvDevice.Refresh(); // Create a new file in C:\\ dir WriteXML(envPath + "\\" + dgvDevice.Rows[e.RowIndex].Cells[2].Value + ".xml", new SettingDevice(dgvDevice.Rows[e.RowIndex].Cells[1].Value.ToString(), dgvDevice.Rows[e.RowIndex].Cells[2].Value.ToString(), Int32.Parse(dgvDevice.Rows[e.RowIndex].Cells[3].Value.ToString()), Int32.Parse(dgvDevice.Rows[e.RowIndex].Cells[4].Value.ToString()))); } }
private void WriteXML(string path, SettingDevice device) { XmlTextWriter textWriter = new XmlTextWriter(path, null); // Opens the document textWriter.WriteStartDocument(); // Write first element textWriter.WriteStartElement("Root"); textWriter.WriteStartElement("Device"); // Write next element textWriter.WriteAttributeString("Name", device.Cops); textWriter.WriteAttributeString("Delay", device.DelayTime.ToString()); textWriter.WriteAttributeString("Limit", device.Limit.ToString()); textWriter.WriteEndElement(); textWriter.WriteEndElement(); // Ends the document. textWriter.WriteEndDocument(); // close writer textWriter.Close(); MessageBox.Show("Cập nhật thành công"); }
private SettingDevice ReadXML(string path) { SettingDevice setting = null; try { XmlDocument xdoc = new XmlDocument(); xdoc.Load(path); foreach (XmlNode node in xdoc.DocumentElement) { setting = new SettingDevice(); setting.Cops = node.Attributes["Name"].Value.ToString(); setting.DelayTime = Int32.Parse(node.Attributes["Delay"].Value.ToString()); setting.Limit = Int32.Parse(node.Attributes["Limit"].Value.ToString()); //MessageBox.Show(first); } } catch (IOException ex) { return(null); } return(setting); }
private void timThietBi() { dgvDevice.Columns.Clear(); DataTable dtDevice = new DataTable(); // Tao cau truc datatable dtDevice.Columns.Add("Chọn", typeof(bool)); dtDevice.Columns.Add("Cổng", typeof(string)); dtDevice.Columns.Add("Mạng", typeof(string)); dtDevice.Columns.Add("Độ trễ", typeof(int)); dtDevice.Columns.Add("Giới hạn", typeof(int)); foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo()) { if (comPort.Description.Contains("PC UI Interface")) { string portName = comPort.Name; //Open communication port SerialPort sPort = objclsSMS.OpenPort(portName, Convert.ToInt32(this.cboBaudRate.Text), Convert.ToInt32(this.cboDataBits.Text), Convert.ToInt32(this.txtReadTimeOut.Text), Convert.ToInt32(this.txtWriteTimeOut.Text)); if (sPort != null) { string copSim = objclsSMS.readSimCode(sPort); if ("\"45201\"".Equals(copSim)) { SettingDevice setting = ReadXML(envPath + "\\" + "Mobifone" + ".xml"); if (setting != null) { dtDevice.Rows.Add(false, portName, setting.Cops, setting.DelayTime.ToString(), setting.Limit.ToString()); } else { dtDevice.Rows.Add(false, portName, "Mobifone", "0", "0"); } } if ("\"45202\"".Equals(copSim)) { SettingDevice setting = ReadXML(envPath + "\\" + "Vinaphone" + ".xml"); if (setting != null) { dtDevice.Rows.Add(false, portName, setting.Cops, setting.DelayTime.ToString(), setting.Limit.ToString()); } else { dtDevice.Rows.Add(false, portName, "Vinaphone", "0", "0"); } } if ("\"45204\"".Equals(copSim)) { SettingDevice setting = ReadXML(envPath + "\\" + "Viettel" + ".xml"); if (setting != null) { dtDevice.Rows.Add(false, portName, setting.Cops, setting.DelayTime.ToString(), setting.Limit.ToString()); } else { dtDevice.Rows.Add(false, portName, "Viettel", "0", "0"); } } } else { //MessageBox.Show("Invalid port settings"); this.statusBar1.Text = "Invalid port settings"; } objclsSMS.ClosePort(sPort); } dgvDevice.DataSource = dtDevice; dgvDevice.Refresh(); } DataGridViewButtonColumn btnSetup = new DataGridViewButtonColumn(); dgvDevice.Columns.Add(btnSetup); btnSetup.Text = "Cài đặt"; btnSetup.Name = ""; btnSetup.UseColumnTextForButtonValue = true; }