本帖最后由 dapsjj 于 2016-11-7 09:03 编辑
您好,我代码按照您说的重写了SelectionRenderer,但是没有生效,请问哪里不对呢?33,34行。213-221行是变背景色的代码。
代码如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.Remoting.Messaging;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using MY.BLL;
- using MY.Common;
- namespace PcSystemMain
- {
- public partial class frmGetActualProduction : Form
-
- {
- #region
- private string connPCDB = null;
- private CommonPara cp = new CommonPara();
- private string flag = "";//初始化被选中的记录的标志位空
- private int checked_count = 0;//计算被选中的checkbox的数量
- private int bin = -1;
- #endregion
- private delegate bool DoWorkDelegate();
- public frmGetActualProduction()
- {
- InitializeComponent();
- this.loadingPanel1.OnRotateStateChanged += new LoadingPanel.RotateStateChangedHandler(loadingPanel1_OnRotateStateChanged);
- SelectionRenderer sr = new SelectionRenderer();
- sprshow.SelectionRenderer = sr;
- }
- private void cmdExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- private void frmGetActualProduction_Load(object sender, EventArgs e)
- {
- try
- {
- connPCDB = cp.GetConString();
- SqlHelper sh = new SqlHelper(connPCDB);
- DataSet ds = sh.ExecuteDatasetSQL("SELECT c1,c2,0 as c3, c4 FROM tabel1 ");
- setSpreadData(ds.Tables[0]);
- }
- catch (Exception)
- {
-
- }
- }
-
- private void setSpreadData(DataTable spdt)
- {
- if (this.sprshow.ActiveSheet.RowCount > 0)
- {
- this.sprshow.ActiveSheet.Rows.Count = 0;
- }
- this.sprshow.ActiveSheet.Rows.Count = spdt.Rows.Count;
- DataRowCollection rows = spdt.Rows;
- int rowCount = rows.Count;
- int nowRow = 0;
- for (int i = 0; i < rowCount; i++)
- {
- sprshow.ActiveSheet.Cells[nowRow, 0].Value = rows[i]["A"].ToString();
- sprshow.ActiveSheet.Cells[nowRow, 1].Value = rows[i]["B"].ToString();
- sprshow.ActiveSheet.Cells[nowRow, 2].Value = rows[i]["C"].ToString();
- sprshow.ActiveSheet.Cells[nowRow, 3].Value = rows[i]["D"].ToString();
- nowRow++;
- }
- }
- private void sprshow_ButtonClicked(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
- {
- checked_count = 0;
-
- for (int i = 0; i < sprshow.Sheets[0].Rows.Count; i++)
- {
- if (checked_count <= 1)
- {
- flag = sprshow.Sheets[0].Cells[i, 2].Value.ToString();//复选框是否被选中
- if (flag == "True")//"True"为选中,"0"为未选中。
- {
- int col4_value = int.Parse(sprshow.Sheets[0].Cells[i, 3].Value.ToString());//第4列
-
- if (col4_value == 1)
- {
- sprshow.Sheets[0].Cells[i, 2].Value = 0;//清除被选的状态
- bin = int.Parse(sprshow.Sheets[0].Cells[i, 0].Value.ToString());
- MessageBox.Show(bin);
- break;
- }
- else
- {
- checked_count++;
- }
- }
- }
- else
- {
- MessageBox.Show("只能选一个.");
- checked_count = 0;
- break;
- }
- }
- }
- void loadingPanel1_OnRotateStateChanged()
- {
- this.Invoke(new Action(delegate()
- {
- this.sprshow.Visible = !this.sprshow.Visible;
- this.Cmdchace.Enabled = !this.Cmdchace.Enabled;
- this.cmdGetActualProduction.Enabled = !this.cmdGetActualProduction.Enabled;
- this.cmdExit.Enabled = !this.cmdExit.Enabled;
- }));
- }
- private bool DoWork()
- {
- System.Threading.Thread.Sleep(10000);
- return true;
- }
- private void DoWorkCallBack(IAsyncResult ar)
- {
- AsyncResult result = (AsyncResult)ar;
- DoWorkDelegate caller = (DoWorkDelegate)result.AsyncDelegate;
- bool bResult = caller.EndInvoke(ar);
- this.loadingPanel1.Stop();
- }
- private void cmdGetActualProduction_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
- {
- return;
- }
- else { //点击的是"OK"按钮
- int count = 0;//计算被选择的复选框数量
- string status = "";
- for (int i = 0; i < sprshow.Sheets[0].Rows.Count; i++)
- {
- status = sprshow.Sheets[0].Cells[i, 2].Value.ToString();//复选框是否被选中
- if (status == "True")//"True"为选中,"0"为未选中。
- {
- count++;
- }
- }
- if (count == 0)
- {
- MessageBox.Show("请选择.");
- return;
- }
- else if (count > 1)
- {
- MessageBox.Show("只能选一个.");
- return;
- }
- else//count == 1
- {
- try
- {
- connPCDB = cp.GetConString();
- SqlHelper sh = new SqlHelper(connPCDB);
- sh.Param = sh.GetNewParam();
- sh.Param.AddParam("@intBin", SqlDbType.Int, bin);
- DataSet ds = sh.ExecuteDatasetSP("AcquireMeterActualResults", ref sh.Param, 3000);
-
- }
- catch (Exception)
- {
-
- }
- }
- }
- }
- private void Cmdchace_Click(object sender, EventArgs e)
- {
-
- for (int i = 0; i < sprshow.Sheets[0].Rows.Count; i++) {
- int col4_value = int.Parse(sprshow.Sheets[0].Cells[i, 3].Value.ToString());//第4列
- if (col4_value==1)
- {
- for (int j = 0; j <= i;j++ )
- {
- sprshow.Sheets[0].Cells[j, 2].Value = 0;
- }
- MessageBox.Show("不能全选.");
- break;
- }
- sprshow.Sheets[0].Cells[i, 2].Value = true;
- }
- }
- }
- public class SelectionRenderer : FarPoint.Win.Spread.ISelectionRenderer
- {
- public void PaintSelection(Graphics g, int x, int y, int width, int height)
- {
- SolidBrush selectionBrush = new SolidBrush(Color.Blue);
- g.FillRectangle(selectionBrush, x, y, width, height);
- selectionBrush.Dispose();
- }
- }
- }
复制代码 |