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
{
}
}
}