Home > Net >  About/DataGridView control control problem of the column value and uniqueness
About/DataGridView control control problem of the column value and uniqueness

Time:04-10

Background:
1, do to DataGridView column head initialization processing:
 
Public static DataGridView InitDgvJobFace (ref DataGridView DGV)
{
DataGridView DgvJob=DGV;

///BranJob column initializes the
Multiselect DataGridViewCheckBoxColumn multiselect=CreateCheckColumn (" ", "select");

DataGridViewTextBoxColumn id=CreaTextBoxColumn (" id ", "identity" of the internal code being used, "BranjobId", false);
DataGridViewComboBoxColumn jobna=CreateComboBoxColumn (" jobna ", "position name" and "Jobno");
SetAlternateChoicesUsingItems (jobna);

DataGridViewTextBoxColumn planqty=CreaTextBoxColumn (" planqty ", "the number of personnel allocation", "planqty");

DataGridViewTextBoxColumn jobqty=CreaTextBoxColumn (" jobqty ", "actual number", "jobqty");
DataGridViewTextBoxColumn lackqty=CreaTextBoxColumn (" lackqty ", "lack of number", "lackqty");

DataGridViewTextBoxColumn overtime=CreaTextBoxColumn (" overtime ", "fixed overtime hours", "overtime");
Overtime. DefaultCellStyle=CellStyle (CellStyleState. DecimalCellStyle);


//added to the DataGridView object
DgvJob. Columns. AddRange (new DataGridViewColumn []
{
Id,
Jobna,
Planqty,
Jobqty,
Lackqty,
Overtime
});
//insert the multiselect [list]
DgvJob. Columns. Insert (0, multiselect);
InitDgv (DgvJob);

Return DgvJob;
}


2, data loading:
 
Public class ContextHelper
{
Public static DataTable GetBranjob (string branno)
{
Try
{
Using (CusProContext db=new CusProContext ())
{
Var BranjobObj=(from p in the db. BranJob
Where p.B ranno==branno
Join a db in JobSet on p.J obno equals a. obno
Select new
{
P.B ranjobId,
P.J obno,
A. obna,
P.P lanqty,
P.O vertime
}
Into x
Group x by new
{
X.B ranjobId,
X.J obno,
X.J obna,
X.P lanqty,
X.o. vertime
}
Into y
Select new
{
Y.K ey BranjobId,
Y.K. Ey Jobno,//position number
Y.K ey Jobna,//job title
Y.K ey Planqty,//the number of personnel allocation
Jobqty=y.C mount (),//actual number
Lackqty=y.K ey. Planqty - y.C mount (),//defective number
Overtime=y.S um (s=& gt; S.O vertime)//fixed overtime hours
}). ToList ();

Return LinqToDataTable (BranjobObj);
}
}
The catch (Exception ex)
{
Throw the ex.
}
}
}

//here is the form DGV controls loading:
Private void InitDgvData (DataGridView DGV)
{
DGV. The DataSource=ContextHelper. GetBranjob (BranObj Branno);
}

Private void Treelst_FocusedNodeChanged (object sender, DevExpress XtraTreeList. FocusedNodeChangedEventArgs e)
{
//... (omitted)
//department position information
InitDgvData (DgvJob);
}


3, to Dgv event handling
 
//new line default
Private void DgvJob_DefaultValuesNeeded (object sender, DataGridViewRowEventArgs e)
{
"E.R ow. Cells [r]." multiselect "Value=https://bbs.csdn.net/topics/e.Row.IsNewRow;
//"e.R ow. Cells [r]." jobna Valuehttps://bbs.csdn.net/topics/="";

//editable
"E.R ow. Cells [r]." jobna "ReadOnly=false;
"E.R ow. Cells [r]." planqty "ReadOnly=false;
"E.R ow. Cells [r]." overtime "ReadOnly=false;
//editable position background
"E.R ow. Cells [r]." jobna "style.css. BackColor=Color. LightYellow;
"E.R ow. Cells [r]." planqty "style.css. BackColor=Color. LightYellow;
"E.R ow. Cells [r]." overtime "style.css. BackColor=Color. LightYellow;
}

//cell edit mode termination event
Private void DgvJob_CellEndEdit (object sender, DataGridViewCellEventArgs e)
{
//press ESC to clear the error message,
DgvJob. Rows [" e.R owIndex] ErrorText=null;

//edit after calculation logic:
Try
{
If (DgvJob. Rows. Count & gt; 1)
{
//position number
String rowJobna=DgvJob. Rows [" e.R owIndex]. Cells [r]. "jobna" Value. The ToString ()?? "";
//the number of personnel allocation
Int rowPlanqty=DgvJob. Rows [" e.R owIndex]. Cells [r]. "planqty" Value!=DBNull. Value? (int) DgvJob. Rows [" e.R owIndex]. Cells [r]. "planqty" Value: 0;
//actual number
DgvJob. Rows [" e.R owIndex] Cells [r]. "jobqty" Value=https://bbs.csdn.net/topics/ContextHelper.FunBranJobqty (BranObj Branno, rowJobna);
Int rowJobqty=(int) DgvJob. Rows [" e.R owIndex]. Cells [r]. "jobqty" Value;

//defective number
DgvJob. Rows [" e.R owIndex] Cells [r]. "lackqty" Value=https://bbs.csdn.net/topics/rowPlanqty - rowJobqty;
//MessageDxUtil. ShowTips (" cell selected value: "+ rowJobqty);
}

}
The catch (Exception ex)
{
MessageDxUtil. ShowTips (ex. Message);
}

}


4, add a new event
 
Private void BarBtnAddUser_ItemClick (object sender, DevExpress XtraBars. ItemClickEventArgs e)
{
ActState=ActionState. DetailAdd;

//judgment TabControl. Page Page focal
If (TabCtl. SelectedTab==TabCtl. TabPages [0])
{
//DgvJob
//new blank lines
DgvJob. AllowUserToAddRows=true;
}

}


Rendering of the UI is like this:


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related