> For the complete documentation index, see [llms.txt](https://kythuat.dtechvn.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kythuat.dtechvn.com/nghiep-vu/code-nut-chuc-nang-gan-du-lieu-xuong-luoi.md).

# CODE NÚT CHỨC NĂNG GÁN DỮ LIỆU XUỐNG LƯỚI

## Yêu cầu

* Tạo nút chức năng trên lưới
* Code nút gán dữ liệu nhiều dòng xuống lưới

## Hướng dẫn thao tác

* Chuột phải vào 1 object có sẵn của hàng nút chức năng trên lưới/ chọn nhân đôi
* chuột phải properties/ sửa name, text
* code chức năng. ở dưới là 2 ví dụ

### Ví dụ 1: code nút nhập số tiền CIF rồi phân bổ xuống lưới

```csharp
/*
		- phân bổ giá cif: bản chất là phí vận chuyển + bảo hiểm do người bán cung cấp. nên cần phân bổ để tính thuế nhập khẩu. 
		- bấm vào nút, hiển thị ô nhập tiền (format kiểu vnd)
		- bấm chấp nhận, thì phân bổ xuống cột cif ở dưới lưới. Theo tỷ lệ: cif_tong * psno / sum(psno) ở dưới lưới.
			+ lưu ý làm tròn số tiền lần cuối cùng.
		- tính lại psnoTinhthueHQ
			+ sau khi soluong*dongia*tygiaHQ
			+ kiểm tra xem hình như còn chi phí phân bổ trước hải quan. nếu có thì cif sau cả đoạn này
			+ cộng thêm cif vừa được phân bổ vào.
		- tính lại các cột liên quan
			+ các loại thuế phía sau thuexnk
		*/
		try
		{
		bool confirm = false; 
		decimal cif = 0;
		//tính tổng lại giá trị cif đã bổ nếu có
		// DataRow dr = _Func.GetFormValues(new Size(300, 400), _Func.DichNN("Nhập số dòng thêm"), 1, out confirm, "N", "Số dòng:#3#0#10");
		if(myform.DTG0001.dvSource.Table.Columns.Contains("CIF"))
		{
			cif = Convert.ToDecimal(myform.DTG0001.dvSource.Table.Compute("Sum(CIF)",""));
			DataRow dr1 = _Func.GetFormValues(new Size(300, 400), "Phân bổ giá CIF", 1, out confirm , "N"
		, "Tiền cif bổ: #18#0#"+ cif.ToString().Trim());
			if(confirm)
			{
				decimal cifbo = Convert.ToDecimal(dr1[0]);
				_Func.EditTable(myform.DTG0001.dvSource.Table,"CIF",0,"");
				if(cifbo >0)
				{
					decimal tongtieuthuc = 0;
					object resul = myform.DTG0001.dvSource.Table.Compute("Sum(PsNo)","PsNo>0");
					if(resul != DBNull.Value)
						tongtieuthuc = Convert.ToDecimal(resul);
					if(tongtieuthuc<=0)
					{
						_Func.ThongBaoLoi("Phí cif bổ theo tiền nên vui lòng nhập tiền của các dòng chi tiết trước khi bổ.");
						return;
					}
					DataRow[] drbo = myform.DTG0001.dvSource.Table.Select("PsNo>0");
					decimal conlai = cifbo;
					for(int i =0 ;i< drbo.Length; i++)
					{
						if(i== drbo.Length-1)
							drbo[i]["CIF"] = conlai;
						else
						{
							decimal gtbo = Math.Round(cifbo* Convert.ToDecimal(drbo[i]["PsNo"])/tongtieuthuc, 0, MidpointRounding.AwayFromZero);
							drbo[i]["CIF"] = gtbo;
							conlai -= gtbo;
						}
					}
				}
				//Chạy lại tính thuexnk cộng thêm phí CIf
				myform.CodeChungNL("ptthuexnk",true);
			}

		}
		
		}catch(Exception ex){MessageBox.Show(ex.Message);}
```

### Ví dụ 2: code nút Xuất vật tư theo báo cáo

```csharp
/*
		- XuatNguyenlieu_NWC: mẫu báo cáo sử dụng
		- chạy báo cáo: tungay,denngay= dauthang,cuoithang của vr.ngay trên giao diện
		- hiển thị brow bảng kết quả. cho phép người dùng chọn hoặc bỏ (dòng, toàn bộ bảng)
		- gán toàn bộ dữ liệu xuống dưới. khớp tên thì gán xuống. (báo cáo để các cột cần gán khớp với tên cấu trúc cột)
		*/
		DateTime tungay = new DateTime(myform.txtNgay.Value.Year, 1, 1);
		DateTime denngay = new DateTime(myform.txtNgay.Value.Year, 1, 1).AddYears(1).AddDays(-1);
		DataTable dtbrow = null;
		DataTable dtDLBC = _Func.ChayBaoCao(0, "XuatNguyenlieu_NWC", "DauBC=0", "", tungay, denngay, out dtbrow, myform);
		if(dtDLBC != null)
		{
			//Thêm cột ok để lựa chọn
			if(!dtDLBC.Columns.Contains("ok"))
			{
				dtDLBC.Columns.Add(new DataColumn() { ColumnName = "ok", DataType = typeof(int), 	DefaultValue = 0 });
				if(!dtDLBC.Columns.Contains("ColorRow"))
					dtDLBC.Columns.Add(new DataColumn() { ColumnName = "ColorRow", DataType = typeof(int), 	DefaultValue = 0 });
				dtDLBC.Columns["ColorRow"].Expression = "ok*2";
				//Thêm brow cột ok
				if(dtbrow != null && dtbrow.Rows.Count>0)
				{
					DataRow drbr = dtbrow.NewRow();
					drbr.ItemArray = dtbrow.Rows[0].ItemArray;
					drbr["Stt"] = 0.01;
					drbr["FieldName"] = "ok";
					drbr["VietNam"] = "ok";
					drbr["Type"] = "N";
					drbr["FromList"] = "0\n1";
					drbr["MultiRow"] = 1;
					dtbrow.Rows.Add(drbr);
				}

				_Func.EditTable(dtbrow, "Width", 80, "");
			}

			ViewTableConfig vtc = new ViewTableConfig();
			vtc.AllowEdit = true;
			vtc.Title = "Lựa chọn dữ liệu";
			vtc.ShowEmptyTable = true;
			vtc.CloseWhenDoubleClick = true;
			vtc.CloseWhenEnter = true;
			vtc.CloseWhenEscape = true;
			vtc.dtviewconfig = dtbrow;
			vtc.AutoSizeColumn = true;
			vtc.ButtonOnView = new string[] { "F7", "F4", "F12", "F5", "F3" };
			_Func.ViewTable(dtDLBC, vtc);
			if(dtDLBC.Select("ok=1").Length>0)
			{
				myform.DTG0001.dvSource.Table.Rows.Clear();
				foreach(DataRow dr in dtDLBC.Select("ok=1"))
				{
					DataRow drnew = myform.DTG0001.dvSource.Table.NewRow();
					//gán mặc định của nghiệp vụ khi thêm dòng mới
					myform.SetDefaultValueToRow(drnew);
					foreach(DataColumn col in dtDLBC.Columns)
					{
						if(drnew.Table.Columns.Contains(col.ColumnName) && dr[col.ColumnName] != DBNull.Value)
							drnew[col.ColumnName] = dr[col.ColumnName];
					}

					drnew.Table.Rows.Add(drnew);
				}

				myform.DTG0001.dvSource.Table.AcceptChanges();
				//Đẩy hiển thị các trường trên form
				if(dtDLBC.Columns.Contains("Kho"))
					myform.txtKhoMa.Value = Convert.ToInt32(dtDLBC.Select("ok=1")[0]["Kho"]);
			}
		}
		else
			_Func.ThongBao("Không có dữ liệu cần xuất. Hãy kiểm tra lại");
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://kythuat.dtechvn.com/nghiep-vu/code-nut-chuc-nang-gan-du-lieu-xuong-luoi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
