public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("");
dt.Columns.Add("姓名");
dt.Columns.Add("年龄");
dt.Columns.Add("性别");
dt.Columns.Add("操作");
DataRow dataRow = dt.NewRow();
dataRow[1] = "Tom";
dataRow[2] = "20";
dataRow[3] = "男";
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dataRow[1] = "Jerry";
dataRow[2] = "10";
dataRow[3] = "女";
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
dataRow = dt.NewRow();
dt.Rows.Add(dataRow);
//在指定位置插入数据源,这种方式可以在多个位置插入数据表,但是需要自己设置列名,且通过FpSpread的DataSource属性无法获取到数据源
//FarPoint.Win.Spread.Data.SpreadDataBindingAdapter data = new FarPoint.Win.Spread.Data.SpreadDataBindingAdapter();
//// Assign the datasource to a data table
//data.DataSource = dt;
//data.Spread = fpSpread1;
//data.SheetName = "Sheet1";
//data.MapperInfo = new FarPoint.Win.Spread.Data.MapperInfo(0, 1, dt.Rows.Count, dt.Columns.Count);
//data.FillSpreadDataByDataSource();
//data.DataSource = dt;
//data.Spread = fpSpread1;
//data.SheetName = "Sheet1";
//data.MapperInfo = new FarPoint.Win.Spread.Data.MapperInfo(30, 1, dt.Rows.Count, dt.Columns.Count);
//data.FillSpreadDataByDataSource();
fpSpread1.DataSource = dt;
FarPoint.Win.Spread.CellType.ComboBoxCellType productCboType = new FarPoint.Win.Spread.CellType.ComboBoxCellType();
productCboType.Items = new string[] { "男", "女" };
//搜索下拉框内容
productCboType.AutoSearch = FarPoint.Win.AutoSearch.SingleCharacter;
productCboType.Editable = true;
fpSpread1.Sheets[0].Cells[0, 3, dt.Rows.Count - 1, 3].CellType = productCboType;
ButtonCellType buttonCellType = new ButtonCellType();
buttonCellType.Text = "删除";
fpSpread1.Sheets[0].Cells[0, 4, dt.Rows.Count - 1, 4].CellType = buttonCellType;
CheckBoxCellType chkboxcell = new FarPoint.Win.Spread.CellType.CheckBoxCellType();
chkboxcell.ThreeState = false;
chkboxcell.TextTrue = "Checked!";
chkboxcell.TextFalse = "Check";
chkboxcell.TextIndeterminate = "Not Sure";
fpSpread1.ActiveSheet.Cells[0, 0, dt.Rows.Count - 1, 0].CellType = chkboxcell;
fpSpread1.ActiveSheet.ColumnHeader.Cells[0, 0].CellType = chkboxcell;
fpSpread1.ActiveSheet.ColumnHeader.Cells[0, 0].Value = false;
fpSpread1.CellClick += (s, e) =>
{
var sheet = fpSpread1.Sheets[0];
if (e.ColumnHeader && e.Row == 0 && e.Column == 0)
{
var value = sheet.ColumnHeader.Cells[0, 0].Value;
sheet.ColumnHeader.Cells[0, 0].Value = !(bool)sheet.ColumnHeader.Cells[0, 0].Value;
for (int i = 0; i < sheet.RowCount; i++)
{
if (sheet.Cells[i, 0].CellType is CheckBoxCellType)
{
sheet.Cells[i, 0].Value = sheet.ColumnHeader.Cells[0, 0].Value;
}
}
}
else
{
if (e.Column == 0 && sheet.Cells[e.Row, e.Column].CellType is CheckBoxCellType)
{
bool colCheck = true;
for (int i = 0; i < sheet.RowCount; i++)
{
if (sheet.Cells[i, e.Column].CellType is CheckBoxCellType)
{
bool cellChk = (bool)sheet.Cells[i, e.Column].Value;
if (!cellChk == false)
{
colCheck = false;
break;
}
}
}
sheet.ColumnHeader.Cells[0, 0].Value = colCheck;
}
}
};
//不能在当前数据源占用单元格之外设置
fpSpread1.Sheets[0].SetValue(6, 0, "哈哈");
MessageBox.Show("cells[1,1].Value=" + fpSpread1.Sheets[0].Cells[1, 1].Value + "\r\ncells[1,2].Value=" + fpSpread1.Sheets[0].Cells[1, 2].Value);
}
private void FpCell_Click(object sender, CellClickEventArgs e)
{
MessageBox.Show("aaa");
}
private void fpSpread1_ComboSelChange(object sender, EditorNotifyEventArgs e)
{
FpCombo cbo = (sender as FpSpread).EditingControl2 as FpCombo;
MessageBox.Show(cbo.SelectedItem.ToString());
}
private void fpSpread1_ButtonClicked(object sender, EditorNotifyEventArgs e)
{
if ((sender as FpSpread).EditingControl2 is FpButton)
{
//删除当前行
fpSpread1.ActiveSheet.RemoveRows(e.Row, 1);
}
if ((sender as FpSpread).EditingControl2 is FpCheckBox)
{
FpCheckBox chk = (sender as FpSpread).EditingControl2 as FpCheckBox;
if (chk.Checked)
{
MessageBox.Show(fpSpread1.Sheets[0].Cells[e.Row, e.Column + 1].Value == null ? "" : fpSpread1.Sheets[0].Cells[e.Row, e.Column + 1].Value.ToString());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = fpSpread1.Sheets[0].DataSource as DataTable;
object value = fpSpread1.ActiveSheet.Cells[0, 0].Value;
}
整个demo示例上传超过20M了,这是所有的.cs文件代码,前台页面只有一个button和FpSpread
|