Bind data to the Grid of SourceGrid, and bind sourcegridgrid
Private void BindData ()
{
// Add a click event for the bound button Line Selection
SourceGrid. Cells. Controllers. CustomEvents clickEvent = new SourceGrid. Cells. Controllers. CustomEvents ();
ClickEvent. Click + = new EventHandler (clickEvent_Click );
// Clear the bound data
This. grid1.Columns. Clear ();
This. grid1.Rows. Clear ();
Grid1.BorderStyle = BorderStyle. FixedSingle;
Grid1.ColumnsCount = 8; // you can specify the number of columns to bind.
// Set the column width
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;
// Set a data row to be Bound each time
Grid1.FixedRows = 1;
// Start binding the first row of data, that is, the column title
Grid1.Rows. Insert (0 );
// Bind the column header to the first row
SourceGrid. Cells. ColumnHeader columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("service name ");
Grid1 [0, 0] = columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("service description ");
Grid1 [0, 1] = columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("Log File ");
Grid1 [0, 2] = columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("download log file ");
Grid1 [0, 3] = columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("configuration file ");
Grid1 [0, 4] = columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("download configuration file ");
Grid1 [0, 5] = columnHeader;
ColumnHeader = new SourceGrid. Cells. ColumnHeader ("Upload configuration file ");
Grid1 [0, 6] = columnHeader;
For (int I = 0; I <sendList. Count; I ++) // SendList is used to provide data. It is a generic set that contains detailed information about multiple services.
{
Grid1.Rows. Insert (I + 1); // Add data to row I + 1 because I starts from 0. If data is bound from I, it overwrites the column header.
Grid1.Rows [I + 1]. Height = 30; // you can specify the Row Height.
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 ));
// Determine whether a configuration file exists. If it does not exist, mark it as none. If it exists, you can download it and upload it to replace the configuration file.
If (sendList [I]. Config = null)
{
Grid1 [I + 1, 4] = new SourceGrid. Cells. Cell ("NONE ");
Grid1 [I + 1, 5] = new SourceGrid. Cells. Cell ("NONE ");
Grid1 [I + 1, 6] = new SourceGrid. Cells. Cell ("NONE ");
}
Else
{
String [] config = sendList [I]. Config. Split (new char [] {'|'}); // used to obtain all the configuration files
// Bind a 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;
// Bind Combox to column 5th
Grid1 [I + 1, 4] = new SourceGrid. Cells. Cell ("", cbEditor );
Grid1 [I + 1, 4]. View = SourceGrid. Cells. Views. ComboBox. Default;
// Bind a Button in column 6th to click to download the configuration file
Grid1 [I + 1, 5] = new SourceGrid. Cells. Button ("download configuration file ");
Grid1 [I + 1, 5]. AddController (clickEvent); // bind a click event to the Button
Grid1 [I + 1, 6] = new SourceGrid. Cells. Button ("Upload configuration file ");
Grid1 [I + 1, 6]. AddController (clickEvent );
}
// Determine whether a log file exists. If no log file exists, mark it as the Download button.
If (sendList [I]. Log = null)
{
Grid1 [I + 1, 2] = new SourceGrid. Cells. Cell ("NONE ");
Grid1 [I + 1, 3] = new SourceGrid. Cells. Cell ("NONE ");
}
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 ("download log file ");
Grid1 [I + 1, 3]. AddController (clickEvent );
}
}
}
// Used to register a specific click event, which can be ignored.
Private void clickEvent_Click (object sender, EventArgs e)
{
SourceGrid. CellContext context = (SourceGrid. CellContext) sender;
// Obtain the service name of the row currently clicked
String str = grid1 [context. Position. Row, 0]. ToString ();
Byte [] buffer;
// If you click the button in the log file column, a message is sent to the server.
If (context. Position. Column = 3)
{
// Determine whether a log file is selected
String file = grid1 [context. Position. Row, 2]. ToString ();
If (file = "")
{
MessageBox. Show ("select the log file to download ");
Return;
}
// When the client sends 0 messages to the server, the log file is downloaded.
String s = "0 ";
S + = str + "|" + file;
Buffer = System. Text. Encoding. UTF8.GetBytes (s );
Try
{
Socket. Send (buffer );
}
Catch
{}
}
// If you click the button in the configuration file column, a message is sent to the server.
If (context. Position. Column = 5)
{
// Determine whether the configuration file is selected
String file = grid1 [context. Position. Row, 4]. ToString ();
If (file = "")
{
MessageBox. Show ("select the configuration file to download ");
Return;
}
// When the client sends 1 message to the server, the log file is downloaded.
String s = "1 ";
S + = str + "|" + file;
Buffer = System. Text. Encoding. UTF8.GetBytes (s );
Try
{
Socket. Send (buffer );
}
Catch
{
}
}
// If you click the upload configuration file column button, send a configuration file to the server to replace the original configuration file.
If (context. Position. Column = 6)
{
// First check whether the configuration file to be modified is selected
String file = grid1 [context. Position. Row, 4]. ToString ();
If (file = "")
{
MessageBox. Show ("select the configuration file to replace ");
Return;
}
Try
{
// Used to open the configuration file to be uploaded
OpenFileDialog ofd = new OpenFileDialog ();
Ofd. InitialDirectory = Environment. GetFolderPath (Environment. SpecialFolder. Exclude topdirectory );
Ofd. Title = "configuration file to be uploaded ";
Ofd. Filter = "configuration file | *. xml | configuration file | *. txt | all files | *.*";
Ofd. ShowDialog (this );
// Obtain the path of the selected file
String path = ofd. FileName;
// Send a file to the server and add a byte array of 2 and service name to the beginning of the configuration file to be sent, so that the server can identify
Using (FileStream fsRead = new FileStream (path, FileMode. Open, FileAccess. Read ))
{
Byte [] buffer2 = new byte [fsRead. Length];
// The actual number of bytes read
Int r = fsRead. Read (buffer2, 0, buffer2.Length );
String s = "2 ";
S = s + str + "|" + file + "| ";
// Declare a collection of bytes to connect the identifiers and files
List <byte> list = new List <byte> ();
Buffer = System. Text. Encoding. UTF8.GetBytes (s );
List. AddRange (buffer );
List. AddRange (buffer2 );
// Convert the combined byte set into a byte array
Byte [] newByte = list. ToArray ();
Socket. Send (newByte );
FsRead. Close ();
}
MessageBox. Show ("uploaded successfully ");
}
Catch
{
}
}
}