private void BindData()
{
//為綁定的按鈕選線增加單擊事件
SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents();
clickEvent.Click += new EventHandler(clickEvent_Click);
//清除原有綁定的數據
this.grid1.Columns.Clear();
this.grid1.Rows.Clear();
grid1.BorderStyle = BorderStyle.FixedSingle;
grid1.ColumnsCount = 8;//設置要綁定的列數
//設置列的寬度
grid1.Columns[0].Width = 200;
grid1.Columns[1].Width = 200;
grid1.Columns[2].Width = 105;
grid1.Columns[3].Width = 105;
grid1.Columns[4].Width = 105;
grid1.Columns[5].Width = 105;
grid1.Columns[6].Width = 105;
//設置每次綁定一行數據
grid1.FixedRows = 1;
//開始綁定第一行數據也就是列標題
grid1.Rows.Insert(0);
//為第一行綁定列頭
SourceGrid.Cells.ColumnHeader columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("服務名稱");
grid1[0, 0] = columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("服務描述");
grid1[0, 1] = columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("日志文件");
grid1[0, 2] = columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("下載日志文件");
grid1[0, 3] = columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("配置文件");
grid1[0, 4] = columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("下載配置文件");
grid1[0, 5] = columnHeader;
columnHeader = new SourceGrid.Cells.ColumnHeader("上傳配置文件");
grid1[0, 6] = columnHeader;
for (int i = 0; i < sendList.Count; i++)//SendList用來提供數據,它是一個泛型集合,裡面包含多個服務的詳細信息
{
grid1.Rows.Insert(i + 1);//為第i+1行增加數據,因為i是從0開始的,如果從i開始綁定數據會覆蓋列頭
grid1.Rows[i + 1].Height = 30;//設置行的高度
grid1[i + 1, 0] = new SourceGrid.Cells.Cell(sendList[i].ServiceName, typeof(string));
grid1[i + 1, 1] = new SourceGrid.Cells.Cell(sendList[i].ServiceDesc, typeof(string));
//判斷是否存在配置文件,如果不存在標記為無,如果存在,可以下載並且可以上傳替換該配置文件
if (sendList[i].Config == null)
{
grid1[i + 1, 4] = new SourceGrid.Cells.Cell("無");
grid1[i + 1, 5] = new SourceGrid.Cells.Cell("無");
grid1[i + 1, 6] = new SourceGrid.Cells.Cell("無");
}
else
{
string[] config = sendList[i].Config.Split(new char[] { '|' });//用來獲得所有的配置文件
//用來綁定一個ComBox
SourceGrid.Cells.Editors.ComboBox cbEditor = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
cbEditor.StandardValues = config;
cbEditor.EditableMode = SourceGrid.EditableMode.Focus | SourceGrid.EditableMode.SingleClick | SourceGrid.EditableMode.AnyKey;
//將Combox綁定到第5列
grid1[i + 1, 4] = new SourceGrid.Cells.Cell("", cbEditor);
grid1[i + 1, 4].View = SourceGrid.Cells.Views.ComboBox.Default;
//第6列綁定一個Button,用來點擊下載配置文件
grid1[i + 1, 5] = new SourceGrid.Cells.Button("下載配置文件");
grid1[i + 1, 5].AddController(clickEvent);//為Button綁定單擊事件
grid1[i + 1, 6] = new SourceGrid.Cells.Button("上傳配置文件");
grid1[i + 1, 6].AddController(clickEvent);
}
//判斷是否存在日志文件,如果不存在標記為無,如果存在,標記為下載按鈕
if (sendList[i].Log == null)
{
grid1[i + 1, 2] = new SourceGrid.Cells.Cell("無");
grid1[i + 1, 3] = new SourceGrid.Cells.Cell("無");
}
else
{
string[] log = sendList[i].Log.Split(new char[] { '|' });
SourceGrid.Cells.Editors.ComboBox cbEditor = new SourceGrid.Cells.Editors.ComboBox(typeof(string));
cbEditor.StandardValues = log;
cbEditor.EditableMode = SourceGrid.EditableMode.Focus | SourceGrid.EditableMode.SingleClick | SourceGrid.EditableMode.AnyKey;
grid1[i + 1, 2] = new SourceGrid.Cells.Cell("", cbEditor);
grid1[i + 1, 2].View = SourceGrid.Cells.Views.ComboBox.Default;
grid1[i + 1, 3] = new SourceGrid.Cells.Button("下載日志文件");
grid1[i + 1, 3].AddController(clickEvent);
}
}
}
//用來注冊具體的單擊事件,可以忽略不看
private void clickEvent_Click(object sender, EventArgs e)
{
SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
//得到當前點擊的行的服務的名稱
string str = grid1[context.Position.Row, 0].ToString();
byte[] buffer;
//如果點擊的是日志文件列的按鈕,則向服務端發送消息提示
if (context.Position.Column == 3)
{
//判斷是否選中了日志文件
string file = grid1[context.Position.Row, 2].ToString();
if (file == "")
{
MessageBox.Show("請選擇要下載的日志文件");
return;
}
//當客戶端向服務端發送0時,代表的時要下載日志文件
string s = "0";
s += str + "|" + file;
buffer = System.Text.Encoding.UTF8.GetBytes(s);
try
{
socket.Send(buffer);
}
catch
{ }
}
//如果點擊的是配置文件列的按鈕,則向服務端發送消息提示
if (context.Position.Column == 5)
{
//判斷是否選中了配置文件
string file = grid1[context.Position.Row, 4].ToString();
if (file == "")
{
MessageBox.Show("請選擇要下載的配置文件");
return;
}
//當客戶端向服務端發送1時,代表的時要下載日志文件
string s = "1";
s += str + "|" + file;
buffer = System.Text.Encoding.UTF8.GetBytes(s);
try
{
socket.Send(buffer);
}
catch
{
}
}
//如果點擊的是上傳配置文件列的按鈕,則向服務器發送一個配置文件替換掉原有的配置文件
if (context.Position.Column == 6)
{
//先檢查是否選中要修改的配置文件
string file = grid1[context.Position.Row, 4].ToString();
if (file == "")
{
MessageBox.Show("請選擇要替換的配置文件");
return;
}
try
{
//用來打開要上傳的配置文件
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
ofd.Title = "要上傳的配置文件";
ofd.Filter = "配置文件|*.xml|配置文件|*.txt|所有文件|*.*";
ofd.ShowDialog(this);
//獲得選中文件的路徑
string path = ofd.FileName;
//向服務器發送文件,向要發送的配置文件的開頭添加2和服務名稱的字節數組,方便服務器用來標識
using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer2 = new byte[fsRead.Length];
//實際讀取到的字節數
int r = fsRead.Read(buffer2, 0, buffer2.Length);
string s = "2";
s = s + str + "|" + file + "|";
//聲明一個字節集合,用來連接標識和文件
List<byte> list = new List<byte>();
buffer = System.Text.Encoding.UTF8.GetBytes(s);
list.AddRange(buffer);
list.AddRange(buffer2);
//將組合好的字節集合轉換為字節數組
byte[] newByte = list.ToArray();
socket.Send(newByte);
fsRead.Close();
}
MessageBox.Show("上傳成功");
}
catch
{
}
}
}