C # WinForm dataGridView skills summary

C # WinForm dataGridView  skills summary 

1 does not show the first blank column
RowHeaderVisible property is set to false 

2. Click the cell to select an entire row
SelectinModel  properties FullRowSelect
RowSelectinModel property
or with CellClick events may be          @ entire row select
Private void dataGridView1_CellClick (Object SENDER, DataGridViewCellEventArgs E)
{
//e.RowIndex> -1 otherwise click header is called an
if (dataGridView1.Rows.Count> 0 && e.RowIndex> -1)
{
//MessageBox.Show (e.RowIndex.ToString ());
dataGridView1.Rows [e.RowIndex] = .selected to true;
}
}
3. Add button picture button bar event
on the button to display the text you want to set the text property. The default is no value. But not figure out why  DataPropertyName bound field is not displayed. So I want to get asID values cognizant recording only into the other columns.
// button event
Private void dataGridView1_CellContentClick (Object SENDER, DataGridViewCellEventArgs E)
{
IF (e.ColumnIndex ==. 1)
{
MessageBox.Show (dataGridView1.Rows [e.RowIndex] .Cells [. 1] .Value.ToString ());
}
}
4. Add button made outside the selected row
Private void the button1_Click (SENDER Object, EventArgs E)
{
IF (dataGridView1.SelectedRows.Count> 0)
{
MessageBox.Show (dataGridView1.SelectedRows [0] .Cells [. 1] .Value. the ToString ());
}
}
5. the other settings.
Choose whether to allow multiple lines: the MultiSelect  .
Do not automatically bound field data table.
Finally, let a width extending over the width of the upper adaptation: setting reclassified AutoSizeModel ofFill

. 6
// the dataGridView  delete a column operation
Private void dataGridView1_CellClick (Object SENDER, DataGridViewCellEventArgs E)
{
IF (e.ColumnIndex == -1)
{
return;
}
String Action = dataGridView1.Columns [e.ColumnIndex] .Name; // operation type
IF (Action == "DelOrder")
{
IF (MessageBox.Show ( " OK to delete it ?", " tips ", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{                 
String ID = dataGridView1.Rows [e.RowIndex] .Cells [. 1] .Value.ToString ();
SellBiClass.DelRules (ID);

                }
}
Newload ();
}

void newload ()
{           
dataGridView1.DataSource SellBiClass.GetRules = ();
}
. 7 // the dataGridView  formatting each column
Private void dataGridView1_CellFormatting (Object SENDER, DataGridViewCellFormattingEventArgs E)
{
IF (e.ColumnIndex ==. 7 ) // which column
{
IF (e.Value.ToString () == ". 1")
{
e.Value = " specific ratio ";              
}
the else
{
e.Value = " default scale ";
e.CellStyle.ForeColor = Color .red;
}
}

        }
8   obtained dataGridView  a primary key of a record id this.dataGridView1 [ first columns , this.dataGridView1.CurrentCell.RowIndex] .Value.ToString ()
. 9  does not show dataGridView1 last line blank  dataGridView1.AllowUserToAddRows = false;  it is provided on statement to black hair

10 contextMenuStrip  properties: Right-click the shortcut control when a note is displayed .

11 导出excel  private void pictureBox4_Click(object sender, EventArgs e)
{
#region 导出
if (dataGridView1.Rows.Count > 0)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "xls";
saveFileDialog.Filter = "EXCEL文件(*.XLS)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.FileName = "客户信息";
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出到EXCEL";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName == "")
return;
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
sw.WriteLine (tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
#endregion
}

12. The calculation of the total number of records  (dataGridView1.Rows.Count> 0) dataGridView1.Rows.Count> -1 comprises a header row

13 using button controls are modified dataGridView  a data line or delete rows of a
Private void pictureBox2_Click (SENDER Object, EventArgs E)
{
#region  assignment
IF (dataGridView1.Rows.Count> 0)
{
EmpInfo empInfo new new EmpInfo = ();
int index = dataGridView1.CurrentRow.Index;

                //记录复制到文本框
empInfo.EmpId = dataGridView1.Rows[index].Cells[0].Value.ToString();
empInfo.EmpName = dataGridView1.Rows[index].Cells[1].Value.ToString();
empInfo.EmpSex = dataGridView1.Rows[index].Cells[2].Value.ToString();
empInfo.EmpPhone = dataGridView1.Rows[index].Cells[3].Value.ToString();
empInfo.EmpDate = Convert.ToDateTime(dataGridView1.Rows[index].Cells[4].Value);
empInfo.EmpPhone = dataGridView1.Rows[index].Cells[5].Value.ToString();
empInfo.EmpEdu = dataGridView1.Rows[index].Cells[6].Value.ToString();
empInfo.EmpMarriage = dataGridView1.Rows[index].Cells[7].Value.ToString();
empInfo.EmpBirth = Convert.ToDateTime(dataGridView1.Rows[index].Cells[8].Value.ToString());
empInfo.EmpCard = dataGridView1.Rows[index].Cells[9].Value.ToString();
empInfo.EmpAddress = dataGridView1.Rows[index].Cells[10].Value.ToString();
empInfo.EmpRemark = dataGridView1.Rows[index].Cells[11].Value.ToString();
EmpNews empNews = new EmpNews(empInfo);
empNews.ShowDialog();
dataGridView1.DataSource = HXX.SeleEmp();
}
#endregion
}

void pictureBox3_Click Private (SENDER Object, EventArgs E)
{
#region  remove
IF (dataGridView1.Rows.Count> 0)
{
int index = dataGridView1.CurrentRow.Index;
String Empld dataGridView1.Rows = [index] .Cells [0] .Value .ToString ();
IF (MessageBox.Show ( " ! will delete can not be restored ", " message ", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
HXX.deleEmp (EmpId);
dataGridView1.DataSource HXX.SeleEmp = ();
}
}
#endregion
}

cited from: http://limingloved.blog.163.com/blog/static/132297658201022454929764/ 

 

Reproduced in: https: //www.cnblogs.com/baishiying/archive/2012/08/17/2644233.html

Guess you like

Origin blog.csdn.net/weixin_33968104/article/details/93439961