Page 1 of 1

เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร

Posted: Sat Dec 14, 2019 11:14 pm
by gaka

Code: Select all

using System;
using System.Data;
using FireSharp.Config;
using FireSharp.Interfaces;
using FireSharp.Response;
using System.Windows.Forms;


namespace Cshrp_doups
{
    public partial class Form1 : Form
    {
        IFirebaseConfig config = new FirebaseConfig
        {
            AuthSecret = "SrqpM36Rj5S6R4ATa4tzYdTPN0EEcgR",
            BasePath = "https://doups.firebaseio.com/"
        };
        IFirebaseClient client;
        
        public Form1()
        {
            InitializeComponent();
        }

        private async void btmsearch_Click(object sender, EventArgs e)
        {
            
            //ค้นหาข้อมูลทำตรงนี
            FirebaseResponse response = await client.GetAsync("customer/" + txtclientsearchkey.Text);
            Customer cust = response.ResultAs<Customer>();
            //debug.print command in vb = System.Diagnostics.Debug.WriteLine("Hello world");
            // or System.Console.WriteLine("Hello world"); 
             if (cust != null)
            {
                Console.WriteLine(cust.custid.ToString());
                dataGridView1.Rows.Clear();
                dataGridView1.Rows.Add(cust.custid, cust.customername, cust.customeraccount, cust.custpreflang);
            }
        }    
        private void button3_Click(object sender, EventArgs e)
        {
            //Add shipment data put <code> here to open transaction window
	}
        private void Form1_Load(object sender, EventArgs e)
        {
            client = new FireSharp.FirebaseClient(config);
            if (client != null)
            {
                //MessageBox.Show("connected");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            DataGridViewRow row = dataGridView1.Rows[rowIndex];
            //txtclientsearchkey.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine(this.comboBox1.Text);
        }

        private void btmaddnew_Click(object sender, EventArgs e)
        {
            Form newcust = new AddCustomer();
            newcust.Show();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
        
            FirebaseResponse respallcust = await client.GetAsync("customer/");
        // ตรงนี้จะได้ข้อมูลออกมาเป็นชุดคล้าย json 
        /*
        **JSON ที่ได้หน้าตาแบบนี้**
{
  "1" : {
    "custid" : "1",
    "customeraccount" : "FEE45GH",
    "customername" : "Bay bank",
    "custpreflang" : "EN"
  },
  "2" : {
    "custid" : "2",
    "customeraccount" : "FT4GGH",
    "customername" : "BBL ",
    "custpreflang" : "EN"
  },
  "77" : {
    "custid" : "77",
    "customeraccount" : "MR4GGS",
    "customername" : "Max77",
    "custpreflang" : "EN"
  },
  "123456789" : {
    "custid" : "123456789",
    "customeraccount" : "FE3CDE",
    "customername" : "Kitjar",
    "custpreflang" : "EN"
  },
  "234569876" : {
    "custid" : "234569876",
    "customeraccount" : "REF2GGY",
    "customername" : "Yanakorn",
    "custpreflang" : "EN"
  },
  "345667JK" : {
    "custid" : "345667JK",
    "customeraccount" : "N/A",
    "customername" : "Somchai",
    "custpreflang" : "EN"
  },
  "8EE9433" : {
    "custid" : "8EE9433",
    "customeraccount" : "8EE9433",
    "customername" : "nothing 8EE9433",
    "custpreflang" : "EN"
  }
}
        */
        
            Data allcust = respallcust.ResultAs<Data>();
            try
            {
                dataGridView1.Rows.Clear();
                //ที่จะถามคือตรงนี้ เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร
                dataGridView1.Rows.Add(allcust.custid, allcust.customername, allcust.customeraccount, allcust.custpreflang);
            }
            catch
            {

            }
        }
    }
  
}
พอดีผมพึ่งหัด c# ผิดยังไงรบกวนชี้แนะด้วยครับ
เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร

Re: เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร

Posted: Sun Dec 15, 2019 12:00 am
by snasui
:D ผมไม่เคยเขียนติดต่อ Filebase ครับ

ลองดูตาม Link นี้เผื่อพอช่วยได้ครับ https://github.com/step-up-labs/firebas ... -485153614

Re: เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร

Posted: Mon Dec 16, 2019 9:46 am
by gaka
ที่เขียน เชื่อมต่อกับฐานข้อมูล เป็นแบบไหนครับ เชื่อมต่อผ่าน database ที่ include เข้ามาใน Project หรือ สร้างเป็น Connection adapter พอดีผม ทดลองใช้ database เป็น access accdb
ตัว odbc Driver ที่ลงในเครื่อง มันใช้ได้แต่ mdb
ยังหาทางแก้ไม่ได้พยายามที่จะไม่ลง database ไว้ในเครื่องใดเครื่องหนึ่ง ไม่ว่าจะเป็น Microsoft SQL หรือ mysql จึงโดดไปใช้บนไฟล์เบส

Re: เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร

Posted: Mon Dec 16, 2019 5:26 pm
by snasui
:D ปกติผมเขียนแบบ Connectionstring ติดต่อ SQL ตามตัวอย่างด้านล่างครับ

Code: Select all

public static void ConnectToNote(Excel.Application App)
{
	string connStr = @"Data source=Server;
					Initial Catalog=Database;
					User ID=user;
					Password=password";
	SqlConnection sqlcon = new SqlConnection(connStr);

	string sqlStr = @"Select * From [whatEver]";
	SqlDataAdapter sqlda = new SqlDataAdapter(sqlStr, sqlcon);
	System.Data.DataTable dtbl = new System.Data.DataTable();
	sqlda.Fill(dtbl);

	Workbook twb = App.ActiveWorkbook;
	Worksheet wks = twb.Worksheets[1];
	int i = 0;
	wks.Range["a1"].Value = dtbl.Columns[0].ColumnName;

	foreach (DataRow row in dtbl.Rows)
	{
		i++;
		wks.Range["a1"].Offset[i,0].Value = row["xID"];               
	}
}

Re: เอาข้อมูลที่ได้จาก firebaseresponse รูปแบบคล้าย ๆ JSON เอามาใส่เป็น fill in datagrideview มีคำสั่งอย่างไร

Posted: Wed Jul 20, 2022 11:57 am
by cheapache
เมื่อทำการดึงข้อมูลจากไฟล์ base แล้วได้เป็น json object มา

ตามความเข้าใจของผมนะครับ ให้สร้างตัว model ที่ระบุ Property name ของข้อมูลที่เราต้องการตัวอย่างเช่น

Code: Select all

public class SomethingModel {
    Jsonproperty["Id"]
    public int Id {get; set;}
    JsonProperty["Name"]
    public string Name {get; set;}
}

var result = JsonSerializer.Serialize<List<SomethingModel>(response);
foreach (var item in result){
	dataGrid.Row.Add(item);
}
ประมาณนี้ครับ